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

这会引发异常:

WaveBadFormat 调用 waveOutOpen

编码类型为 NAudio 的“MpegLayer3”。

如何使用 NAudio 播放 mp3 文件?

4

3 回答 3

48

对于 NAudio 1.6 及以上版本的用户,请不要使用原始接受答案中的代码。您不需要添加 aWaveFormatConversionStream或 a BlockAlignReductionStream,并且应避免使用WaveOutwith 函数回调(WaveOutEvent如果您不在 WinForms 或 WPF 应用程序中,则更可取)。此外,除非您想阻止播放,否则您通常不会在音频完成之前睡觉。只需订阅WaveOut'PlaybackStopped事件。

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

var reader = new Mp3FileReader("test.mp3");
var waveOut = new WaveOut(); // or WaveOutEvent()
waveOut.Init(reader); 
waveOut.Play();
于 2013-09-09T14:38:58.250 回答
10

试试这样:

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 上使用。请参阅替代答案。

于 2010-03-21T19:47:57.843 回答
0

我喜欢用 NAudio 播放任何 MP3 文件的方法是这样。我更喜欢阻止正在播放的线程,直到 Playback 因事件侦听器而停止。此外,为了获得最佳兼容性,我使用 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;
        }
    }
}

于 2020-04-14T09:55:18.297 回答