我有一个小型 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() 方法时,我的程序都会挂起。它不返回任何错误,它只是挂起。
我究竟做错了什么?