0

I followed the tutorial on NAudio's website on how to load and play an mp3 file, but even though I put the audio file in the right directory, whenever I run, the program crashes with "vshost32.exe has stopped working." Any ideas? I'm using Visual Studio 10.0 on Windows 7.

Here's the (exact) code that the tutorial gave me:

   namespace NAudioTest
   {
       class Program
    {
    static IWavePlayer waveOutDevice;
    static WaveStream mainOutputStream;
    static WaveChannel32 volumeStream;

    static void Main(string[] args)
    {
        waveOutDevice = new WaveOut();
        mainOutputStream = CreateInputStream("Kalimba.mp3");
        waveOutDevice.Init(mainOutputStream);
        waveOutDevice.Play();
    }

    private static WaveStream CreateInputStream(string filename)
    {
        WaveChannel32 inputStream;
        if (filename.EndsWith(".mp3"))
        {
            WaveStream mp3Reader = new Mp3FileReader(filename);
            inputStream = new WaveChannel32(mp3Reader);
        }
        else
        {
            throw new InvalidOperationException("Unsupported extension");
        }
        volumeStream = inputStream;
        return volumeStream;
    }

}
}

(sorry for the poor formatting)

m00nbeam360
  • 1,357
  • 2
  • 21
  • 36

1 Answers1

0

Never mind, used this code to get the audio file to work:

How to play a MP3 file using NAudio

 class Program
 {
     static void Main()
     {
         using (var rdr = new Mp3FileReader(filename))
         using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
         {
              waveOut.Play();
              while (waveOut.PlaybackState == PlaybackState.Playing)
              {
                  Thread.Sleep(100);
              }
         }
     }
  }

Is this better? Although I'm not really sure where WaveOutEvent goes. Thanks for all of your help!!

Community
  • 1
  • 1
m00nbeam360
  • 1,357
  • 2
  • 21
  • 36
  • I wouldn't recommend this technique with versions of NAudio newer than 1.3. Get rid of ms, wavStream and baStream - all are unnecessary. Also, WaveOutEvent will be more reliable than using FunctionCallbacks. – Mark Heath Mar 11 '13 at 10:05
  • Awesome, found another source using WaveOutEvent, much better. Thanks for creating this! – m00nbeam360 Mar 12 '13 at 16:34
  • @m00nbeam360: Can you provide it (Link or code) for everyone? –  Apr 12 '18 at 10:38