waveOutOpen()API中的回调函数 [英] callback funtion in waveOutOpen() API

查看:596
本文介绍了waveOutOpen()API中的回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建音频播放器,并且从waveOutOpen()API调用的回调函数有问题.

打开输出音频设备进行播放:

I am building an audio player and have a problem with the callback function called from waveOutOpen() API.

Opening the output audio device for playback:

MMRESULT mRes = waveOutOpen(m_hWO,WAVE_MAPPER,&wFmt,(DWORD)&waveOutProc,(DWORD)this, CALLBACK_FUNCTION);


在这里,我将"this"作为参数传递给回调函数,因为我必须在不准备缓冲区并关闭Wave-out设备的同时访问类的成员变量.

在waveplay.h中声明回调函数


Here, I am passing the ''this'' as parameter to the call back function, since i have to access the member variables of the class while unpreparing the buffer and closing the wave-out device.

declaration of callback function in waveplay.h

static void (CALLBACK waveOutProc)(HWAVEOUT m_hWO, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);



回调函数的实现:



Implementation of the callback function:

void CPlayWave::waveOutProc(HWAVEOUT m_hWO,UINT uMsg,DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
    MMRESULT mmRes;
    CPlayWave *pPW = (CPlayWave*)dwInstance;
    switch(uMsg)
    {
    case MM_WOM_DONE:       //playback finished
        mmRes = waveOutUnprepareHeader(m_hWO, &pPW->m_WHdr, sizeof(WAVEHDR));
        if(mmRes!=MMSYSERR_NOERROR)
        {
            //error handling
            .....
        }
        mmRes = waveOutClose(m_hWO);
        if(mmRes!=MMSYSERR_NOERROR)
        {
            //error handling
            .....
        }
        AfxMessageBox("Finished playing the file");
        m_bPlay = FALSE;      //boolean flag used for pausing
        break;
    case WIM_DATA:
        //for recording completion
        break;
    }
}



问题是在文件播放完成后,MM_WOM_DONE永远不会发生,并且永远不会调用回调函数.如果必须使用线程来代替回调函数,有人可以给我一个简单的示例来说明如何使用回调线程(在网上找不到).

同样, waveOutReset()文档建议关闭所有缓冲区并返回系统,因此为了处理我的应用程序中的停止"按钮,我使用了waveOutReset()函数,但是这导致应用程序冻结.为什么会这样呢?还有什么其他方法可以在缓冲区仍在播放队列中时停止播放.



The problem is the MM_WOM_DONE never occurs and the callback function is never called after the playback of the file is completed. If a thread has to be used instead of callback function, can someone give me a simple example on how to use a callback thread(haven''t found on net).

Also waveOutReset() documentation suggests that it closes all the buffers and returns to the system, so for handling the Stop-button in my application, I used the waveOutReset() function but, this causing the application to freeze. Why is this happening? Is there any alternative method to stop playing while buffer is still in queue for playback.

推荐答案

waveOutOpen函数
The waveOutOpen function documentation[^] states that the callback receives the following messages:

WOM_OPEN
WOM_CLOSE
WOM_DONE
WOM_OPEN
WOM_CLOSE
WOM_DONE



不是您的代码所期望的.



Not the ones your code expects.


这篇关于waveOutOpen()API中的回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆