waveOutOpen hangs after waveOutClose in C#

800 Views Asked by At

I have a small c# application that uses the waveout interface to periodically call waveoutwrite for writing audio data to the soundcard. I do not use NAudio because I need to use a fixed buffersize of 8192 bytes.

I use the mmdll library in a wrapper class called WaveNative:

// 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);

Now I open the device by calling:

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

This works and I can write audio data to the device. BUT: When the sound has finished playing, I wait for the callback method to signal that all my audio buffers (I have 2 of them, each 8192 bytes) have finished playing:

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

Now, anytime I try to call the waveOutOpen() method again, my program just hangs. It does not return any error, it just hangs.

What am I doing wrong?

0

There are 0 best solutions below