简体   繁体   English

使用NAudio播放mp3文件

[英]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." 我遵循了NAudio网站上有关如何加载和播放mp3文件的教程,但是即使我将音频文件放在正确的目录中,无论何时运行,程序都会因“ vshost32.exe停止工作”而崩溃。 Any ideas? 有任何想法吗? I'm using Visual Studio 10.0 on Windows 7. 我在Windows 7上使用Visual Studio 10.0。

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 如何使用NAudio播放MP3文件

 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. 尽管我不太确定WaveOutEvent的去向。 Thanks for all of your help!! 感谢您所有的帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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