This project has moved and is read-only. For the latest updates, please go here.

WaveIn to SoundPlayer issue.

Apr 18, 2012 at 5:42 AM
Edited Apr 18, 2012 at 5:45 AM

Hello,

I'm trying to use NAudio to capture mic sound and be able to play it in SoundPlayer class. Sound player expects a memory stream with a wav file in it. This is the function using SoundPlayer:

 

 public static void PlaySoundByteData(byte[] data)
        {
            try
            {
                using (Stream s = new MemoryStream(data))
                {
                    // http://msdn.microsoft.com/en-us/library/ms143770%28v=VS.100%29.aspx
                    SoundPlayer myPlayer = new SoundPlayer(s);
                    myPlayer.Play();
                }
            }
            catch
            {
                //Do Nothing
            }
        }
    }

Per VoiceRecorder example project in the site, I am using the WaveIn class buffer data (byte[]) to pass into the function above, but it throws an exception claiming corrupt headers. I don't want to save the buffer in the disk (at least just yet). I want to be able to use the function above as it is being globally for other sound wave streams. For reference I am using AudioRecorder class in the VoiceRecorder project without any changes (other than capturing the WaveIn buffer). Can anyone please suggest a way to achieve this. 

Apr 26, 2012 at 4:55 PM

NAudio has its own playback mechanisms so if you are using NAudio, you would not normally also use SoundPlayer. You could use WaveFileWriter to create a WAV file in a MemoryStream, but you can't append to that stream while you are playing, since the WAV header must know the file length in advance.