How to play a MP3 file using NAudio
WaveStream waveStream = new Mp3FileReader(mp3FileToPlay);
var waveOut = new WaveOut();
waveOut.Init(waveStream);
waveOut.Play();
This throws an excepti开发者_运维技巧on:
WaveBadFormat calling waveOutOpen
The encoding type is "MpegLayer3" as NAudio.
How can I play a mp3 file with NAudio?
For users of NAudio 1.6 and above, please do not use the code in the original accepted answer. You don't need to add a WaveFormatConversionStream
, or a BlockAlignReductionStream
, and you should avoid using WaveOut
with function callbacks (WaveOutEvent
is preferable if you are not in a WinForms or WPF application). Also, unless you want blocking playback, you would not normally sleep until audio finishes. Just subscribe to WaveOut
's PlaybackStopped
event.
The following code will work just fine to play an MP3 in NAudio:
var reader = new Mp3FileReader("test.mp3");
var waveOut = new WaveOut(); // or WaveOutEvent()
waveOut.Init(reader);
waveOut.Play();
Try like this:
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);
}
}
}
}
Edit this code is now out of date (relates to NAudio 1.3). Not recommended on newer versions of NAudio. Please see alternative answer.
my preferred way to play any MP3 files with NAudio is this. I prefer to block the playing thread until Playback stopped with event listeners. Also, for the best compatibility, I use MP3Sharp to load the MP3 file and then pass it to NAudio since NAudio did not come with MP3 codecs.
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;
}
}
}
精彩评论