简体   繁体   中英

Playing mp3 file with NAudio

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)

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM