如何使用NAudio播放MP3文件

时间:2010-03-21 19:35:21

标签: c# mp3 naudio

WaveStream waveStream = new Mp3FileReader(mp3FileToPlay);
var waveOut = new WaveOut();
waveOut.Init(waveStream); 
waveOut.Play();

这会引发异常:

  

WaveBadFormat调用waveOutOpen

编码类型为“MpegLayer3”为NAudio。

如何用NAudio播放mp3文件?

3 个答案:

答案 0 :(得分:35)

对于NAudio 1.6及以上版本的用户,请不要使用原始接受答案中的代码。您不需要添加WaveFormatConversionStreamBlockAlignReductionStream,并且应该避免使用带有函数回调的WaveOut(如果您不在WinForms中,则WaveOutEvent更可取或WPF申请)。此外,除非您想要阻止播放,否则通常不会睡眠,直到音频结束。只需订阅WaveOut的{​​{1}}活动。

以下代码可以很好地在NAudio中播放MP3:

PlaybackStopped

答案 1 :(得分:8)

试试这样:

class Program
{
    static void Main()
    {
        using (var ms = File.OpenRead("test.mp3"))
        using (var rdr = new Mp3FileReader(ms))
        using (var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
        using (var baStream = new BlockAlignReductionStream(wavStream))
        using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
        {
            waveOut.Init(baStream);
            waveOut.Play();
            while (waveOut.PlaybackState == PlaybackState.Playing)
            {
               Thread.Sleep(100);
            }
        }
    }
}

编辑此代码现已过期(与NAudio 1.3相关)。不推荐用于较新版本的NAudio。请参阅替代答案。

答案 2 :(得分:0)

使用NAudio播放任何MP3文件的首选方式是这样。我更喜欢阻塞播放线程,直到事件监听器停止播放为止。另外,为了获得最佳兼容性,我使用MP3Sharp加载MP3文件,然后将其传递给NAudio,因为NAudio并未随附MP3编解码器。

using System;
using NAudio.Wave;
using System.Threading;
using MP3Sharp;
using System.IO;

namespace jessielesbian.NAudioTest
{
    public static class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("loading and parsing MP3 file...");
            MP3Stream stream = new MP3Stream("c:\\workspaces\\Stunning!  Boeing's 737 MAX on Flying Display.mp3");
            WaveFormat waveFormat = new WaveFormat(stream.Frequency, stream.ChannelCount);
            Console.WriteLine("allocating playback cache...");
            FastWaveBuffer fastWaveBuffer = new FastWaveBuffer(waveFormat, (int) stream.Length);
            Console.WriteLine("populating playback cache...");
            stream.CopyTo(fastWaveBuffer);
            fastWaveBuffer.Seek(0, SeekOrigin.Begin);
            Console.WriteLine("unloading MP3 file...");
            stream.Dispose();
            Console.WriteLine("prepairing player...");
            WaveOutEvent waveOutEvent = new WaveOutEvent();
            waveOutEvent.Init(fastWaveBuffer);
            waveOutEvent.Volume = 1;
            Console.WriteLine("arming ManualResetEvent...");
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            waveOutEvent.PlaybackStopped += (object sender, StoppedEventArgs e) => {
                manualResetEvent.Set();
            };
            Console.WriteLine("done!");
            waveOutEvent.Play();
            manualResetEvent.WaitOne();
        }
    }
    public sealed class FastWaveBuffer : MemoryStream, IWaveProvider
    {
        public FastWaveBuffer(WaveFormat waveFormat, byte[] bytes) : base(bytes)
        {
            WaveFormat = waveFormat;
        }
        public FastWaveBuffer(WaveFormat waveFormat, int size = 4096) : base()
        {
            WaveFormat = waveFormat;
            Capacity = size;
        }
        public WaveFormat WaveFormat
        {
            get;
        }
    }
}