-1

我正在使用 waveInProc 回调进行音频采样。问题是,当我尝试停止采样并关闭音频设备时,我在回调中没有收到任何消息 - 尝试了 waveInStop、waveInClose、waveInReset。

请建议。10xs,那鸿

HWAVEIN  hWaveIn
waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,(DWORD)waveInProc,0, CALLBACK_FUNCTION);
waveInStart(hWaveIn);

waveInStop(hWaveIn); //OR
waveInClose(hWaveIn); //OR
waveInReset(hWaveIn); //OR

更新:这是代码:

开始:

waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,(DWORD)waveInProc,0, CALLBACK_FUNCTION);
waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
waveInAddBuffer(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
waveInStart(hWaveIn);

 void CALLBACK waveInProc(  HWAVEIN hwi, UINT uMsg, DWORD dwInstance, 
                            DWORD dwParam1,   DWORD       dwParam2    )
  {

     if (uMsg == WIM_OPEN)
     {
     return;
     }
     if (uMsg == WIM_DATA)
     {
     //process data
     waveInAddBuffer(hWaveIn,(PWAVEHDR)dwParam1,sizeof(WAVEHDR));
     return;
     }
     if (uMsg == WIM_CLOSE) //NOT GETTING THIS MSG

     {
      printf("*****************got WIM_CLOSE\n");
     }
 }

那么如何停止采样并关闭音频设备呢?


这是代码:开始:

waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,(DWORD)waveInProc,0, CALLBACK_FUNCTION);
waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
waveInAddBuffer(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
waveInStart(hWaveIn);

 void CALLBACK waveInProc(  HWAVEIN hwi, UINT uMsg, DWORD dwInstance, 
                            DWORD dwParam1,   DWORD       dwParam2    )
  {

     if (uMsg == WIM_OPEN)
     {
     return;
     }
     if (uMsg == WIM_DATA)
     {
     //process data
     waveInAddBuffer(hWaveIn,(PWAVEHDR)dwParam1,sizeof(WAVEHDR));
     return;
     }
     if (uMsg == WIM_CLOSE) //NOT GETTING THIS MSG

     {
      printf("*****************got WIM_CLOSE\n");
     }
 }

那么如何停止采样并关闭音频设备呢?

10xs,那鸿

4

1 回答 1

1

你在检查你的waveInOpen结果吗?

因为它按预期工作:

hWaveIn 0x005B7768, nMessage 0x03BE, nInstance 0, nParameter1 0, nParameter2 0
nWaveInOpenResult 0, hWaveIn 0x005B7768
hWaveIn 0x005B7768, nMessage 0x03BF (WIM_CLOSE), nInstance 0, nParameter1 0, nParameter2 0

代码:

#include "stdafx.h"
#include <mmsystem.h>

#pragma comment(lib, "winmm.lib")

VOID CALLBACK waveInProc(HWAVEIN hWaveIn, UINT nMessage, DWORD_PTR nInstance, DWORD_PTR nParameter1, DWORD_PTR nParameter2)
{
    _tprintf(_T("hWaveIn 0x%p, nMessage 0x%04X, nInstance %d, nParameter1 %d, nParameter2 %d\n"), hWaveIn, nMessage, nInstance, nParameter1, nParameter2);
}

int _tmain(int argc, _TCHAR* argv[])
{
    WAVEFORMATEX Format = { WAVE_FORMAT_PCM, 1, 8000, 16000, 2, 16, 0 };
    HWAVEIN hWaveIn = NULL;
    const MMRESULT nWaveInOpenResult = waveInOpen(&hWaveIn, WAVE_MAPPER, &Format, (DWORD_PTR) &waveInProc, 0, CALLBACK_FUNCTION);
    _tprintf(_T("nWaveInOpenResult %d, hWaveIn 0x%p\n"), nWaveInOpenResult, hWaveIn);
    waveInStart(hWaveIn);
    waveInStop(hWaveIn);
    waveInClose(hWaveIn);
    return 0;
}

在实际代码中处理数据时,请务必考虑到这一点:在回调函数中

应用程序不应从回调函数内部调用任何系统定义的函数,EnterCriticalSection、LeaveCriticalSection、midiOutLongMsg、midiOutShortMsg、OutputDebugString、PostMessage、PostThreadMessage、SetEvent、timeGetSystemTime、timeGetTime、timeKillEvent 和 timeSetEvent 除外。 调用其他波函数会导致死锁

要重新添加缓冲区,您需要通过使用PostMessageor向另一个线程发出信号来指示需要SetEvent,以便回调之外的代码可以接收此指示并从那里重新添加空缓冲区。

于 2012-08-13T14:05:43.660 回答