waveOutOpen在C#中的waveOutClose之后挂起

时间:2017-12-16 07:58:29

标签: c# windows audio waveout

我有一个小型的c#应用程序,它使用waveout接口定期调用waveoutwrite将音频数据写入声卡。 我不使用NAudio,因为我需要使用8192字节的固定缓冲区大小。

我在名为WaveNative的包装类中使用mmdll库:

// native calls
[DllImport(mmdll)]
public static extern int waveOutGetNumDevs();
[DllImport(mmdll)]
public static extern int waveOutPrepareHeader(IntPtr hWaveOut, ref WaveHdr lpWaveOutHdr, int uSize);
[DllImport(mmdll)]
public static extern int waveOutUnprepareHeader(IntPtr hWaveOut, ref WaveHdr lpWaveOutHdr, int uSize);
[DllImport(mmdll)]
public static extern int waveOutWrite(IntPtr hWaveOut, ref WaveHdr lpWaveOutHdr, int uSize);
[DllImport(mmdll)]
public static extern int waveOutOpen(out IntPtr hWaveOut, int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags);
[DllImport(mmdll)]
public static extern int waveOutReset(IntPtr hWaveOut);
[DllImport(mmdll)]
public static extern int waveOutClose(IntPtr hWaveOut);
[DllImport(mmdll)]
public static extern int waveOutPause(IntPtr hWaveOut);
[DllImport(mmdll)]
public static extern int waveOutRestart(IntPtr hWaveOut);
[DllImport(mmdll)]
public static extern int waveOutGetPosition(IntPtr hWaveOut, out int lpInfo, int uSize);
[DllImport(mmdll)]
public static extern int waveOutSetVolume(IntPtr hWaveOut, int dwVolume);
[DllImport(mmdll)]
public static extern int waveOutGetVolume(IntPtr hWaveOut, out int dwVolume);

现在我通过调用以下方式打开设备:

int msg = WaveNative.waveOutOpen(out myWaveOutHandleAsIntPtr, device, waveformatOfAudioFile, callbackDelegate, 0, WaveNative.CALLBACK_FUNCTION);

这有效,我可以将音频数据写入设备。 但: 当声音播放完毕后,我等待回调方法发出信号表示我所有的音频缓冲区(我有2个,每个8192个字节)已经完成播放:

// Inside the callback method:
if (callbackswaiting > 0)
{
    callbackswaiting--;
    if(callbackswaiting == 0)
    {
        WaveNative.waveOutReset(myWaveOutHandleAsIntPtr);
        WaveNative.waveOutClose(myWaveOutHandleAsIntPtr);
    }
}

现在,只要我再次尝试调用waveOutOpen()方法,我的程序就会挂起。它不会返回任何错误,只是挂起。

我做错了什么?

0 个答案:

没有答案