Using NAudio with the WinMM.dll

May 18, 2011 at 9:10 PM

Hi all. 

I am developing a WAV stream player. Wav stream has been converted from MP3 file using NAudio. But when I try to stop the player I got some random exception. I copied some of my codes below.


1. Creating WAV stream from MP3 file

WaveFormatConversionStream conversionStream = new WaveFormatConversionStream(new WaveFormat(22050, 16, 1), WaveFormatConversionStream.CreatePcmStream(mp3File));

2. Reading it into short array. Because in my music player I am using short[] to play and visualize it.
public int Read(short[] buffer, int readSize)
            byte[] byteBuffer = new byte[buffer.Length * 2];
            int r;
            if ((r = conversionStream.Read(byteBuffer, 0, readSize * 2)) != 0)
                Buffer.BlockCopy(byteBuffer, 0, buffer, 0, byteBuffer.Length);
            return r / 2;

3. Filling my buffer in player in music player class
while ((readSize == reader.Read(readBuffer, readSize))) // reader is my MP3toWavConverter. The read function returns the wav stream chunk.
	readBuffer.CopyTo(waveBuffer, offset); // here waveBuffer is used for playing music

4. Preparing and playing stream
		hHeaderData = GCHandle.Alloc(waveBuffer, GCHandleType.Pinned);
                waveHeader.lpData = hHeaderData.AddrOfPinnedObject();
                waveHeader.dwBufferLength = length;
                waveHeader.dwFlags = 0;
		WinMM.waveOutPrepareHeader(hWaveOut, ref waveHeader, Marshal.SizeOf(waveHeader));
	       WinMM.waveOutWrite(hWaveOut, ref waveHeader, Marshal.SizeOf(waveHeader));

5. WinMM.waveOutReset(hWaveOut); // BOOM. RANDOM ERROR
I can play, pause, open and close WAV stream. But when I try to stop the music player, this function gives random exception. I searched the internet and found out buffering is causing this. is there anything I should be careful about NAUDIO when I try to use it with unmanaged DLLs? How to manage my buffers in NAudio?

May 18, 2011 at 9:12 PM

can you post the exception.ToString()

May 18, 2011 at 9:33 PM

Hi Mark.

The application just crashes down. This is unhandled exception and just opens the Just In time debugger for me. 

I suspect I am not handling the resources correctly. How do I manage my resources when I use NAudio with the unmanaged libraries such as WinMM.dll? I could play WAV file without using NAudio. It worked completely fine. 

May 18, 2011 at 9:35 PM

NAudio already contains wrappers for WinMM.dll. Just create a WaveOut object and pass your conversion stream into it in the Init method. Then call Play()


May 18, 2011 at 9:42 PM

But the thing is I am using this stream for playing and drawing graph for the same time. When I use WaveOut.Play() method it changes the Position. So I can`t draw the correct graph. 

May 18, 2011 at 9:46 PM

Well the code in NAudio WaveOut and WaveBuffer classes represents my best efforts at safely wrapping the WinMM APIs. I would suggest looking at the source code of those classes and seeing if you can use any of the ideas.


May 18, 2011 at 9:48 PM

OK. Thank you Mark.