MP3

Playing an MP3

1) Add the following using statements to your project:

using NAudio;
using NAudio.Wave;

2) Create the declarations in your class:

//Declarations required for audio out and the MP3 stream
IWavePlayer waveOutDevice;
AudioFileReader audioFileReader;

3) In a method – that only needs to be called once, setup the waveOutDevice. In this example we will use WaveOut, since it is the most universally available. If you are not playing audio from within a WinForms or WPF application, then WaveOutEvent may be a better choice.

waveOutDevice = new WaveOut();

4) Load the MP3 file in the same method

audioFileReader = new AudioFileReader("Horrorshow - Thoughtcrime (Doin' My Think).mp3");

 

5)  Init the device and call play – again this also only needs to be called once.

waveOutDevice.Init(audioFileReader);
waveOutDevice.Play();

6) That’s all there is to it. A MP3 playing from your application.
To clean up the code remove the references etc.

private void CloseWaveOut()
{
    if (waveOutDevice != null)
    {
        waveOutDevice.Stop();
    }
    if (mainOutputStream != null)
    {
        audioFileReader.Dispose();
        audioFileReader = null;
    }
    if (waveOutDevice != null)
    {
        waveOutDevice.Dispose();
        waveOutDevice = null;
    }
}

Last edited Dec 21, 2013 at 8:01 PM by markheath, version 14

Comments

ggsmartboy Feb 7, 2013 at 5:21 AM 
How to convert an wav file to mp3 using Naudio or can we able to convert the wave file to mp3 using Naudio. . .

eejake52 Sep 20, 2012 at 9:35 PM 
To answer my own question: MP3, WMA, and AIFF are possible; no sign of M4A.
Here is my version of CreateInputStream:
Private Function CreateInputStream(ByRef fileName As String) As WaveStream
Dim inputStream As WaveChannel32
Dim fReader As WaveStream
If fileName.EndsWith(".aiff") Then
fReader = New AiffFileReader(fileName)
ElseIf fileName.EndsWith(".mp3") Then
fReader = New Mp3FileReader(fileName)
ElseIf fileName.EndsWith(".wma") Then
fReader = New WMAFileReader(fileName)
Else
Throw New InvalidOperationException("Invalid file extension")
End If
inputStream = New WaveChannel32(fReader)
Return inputStream
End Function

eejake52 Sep 20, 2012 at 8:28 PM 
I created a player using VB.NET. It was very easy to follow your example.
It only plays MP3 files. What other file types are supported/allowed? I am hoping for M4A and AIFF; WMA would be nice but not essential.

leye0 May 16, 2012 at 3:20 AM 
Doesn't work here. In a 4.0 WPF, it loads painfully, then the mp3 is playing chunk by chunk.

pyrotrojan May 6, 2012 at 9:23 AM 
So what do we need the volumestream for? Manipulating the volume?

bcekocbit Apr 21, 2012 at 7:14 PM 
I made a console app, it contains class that i called MP3, this class contains methods CreateInputStream and CloseWaveOut from this article, there's also Listen method:
public void Listen()
{
waveOutDevice = new DirectSoundOut();
mainOutputStream = CreateInputStream(FN);
waveOutDevice.Init(mainOutputStream);
waveOutDevice.Play();
Console.Write("Playing...");
CloseWaveOut();
}
in Main method of Program i'm making a copy of MP3, linking it to existing mp3-file, then i call Listen method
and there's no sound, i've tried to initialize waveOutDevice as WaveOut, WaveOutEvent - the same result,
is there any solution, what am i doing wrong?

janrathmer Apr 11, 2012 at 11:03 AM 
Hi Everybody!
I think this is a simple question, but i searched about 1 1/2 hours and didn't find a solution.
How du I change the Volume in a IWavePlayer? I already tried the "Volume" statement of IWavePlayer and WaveChannel32. The Value changed (I tracked it in the Debugging Mode) but nothing happened. The Volume of my earphones wasn't changing!!!...Please help me :) Thanks!

yangfei Dec 16, 2011 at 8:10 AM 
My email is 252264138@qq.com

yangfei Dec 16, 2011 at 8:05 AM 
Hello, I am from China, my English is not very good, I think I need your help, and I need some help,

I want to change the format of audio files

The original audio file format

Bit rate 256Kbps

The audio sample size 16

Audio sampling level 16KHZ

Audio formats PCM

Convert to

Bit rate 64Kbps

The audio sample size 8

Audio sampling level 8KHZ

Audio formats CCITT A-Law

However, I met some problems, abnormal system prompts the Naudio.MmException AcmNotPossible calling acmStreamOpen

I test code is such

NAudio.Wave.WaveFormat format = NAudio.Wave.WaveFormat.CreateALawFormat ( 8000, 1);

Try

{

NAudio.Wave.WaveFileReader fileread = new WaveFileReader ( @" E: \ 1.wav" );

NAudio.Wave.WaveStream WaveStream = new WaveFormatConversionStream ( format, fileread );

NAudio.Wave.WaveFileWriter.CreateWaveFile ( @" E: \ 2.wav", WaveStream );

Console.WriteLine (" OK" );

}

Catch ( ExecutionEngineException Ex )

{

Console.WriteLine ( ex.ToString ( ) );

}

Console.ReadKey ( );

Excuse me, what is wrong.

SethEden Oct 30, 2011 at 4:00 PM 
This discribes what to use for Vista and Win7, but what do you use for WinXP?
3) In a method – that only needs to be called once, setup the waveOutDevice. In this example we will use WASPI – which is available on Vista and Windows 7.
waveOutDevice = new WasapiOut(AudioClientShareMode.Shared, 100);

SectionTwelve Jun 30, 2011 at 2:13 PM 
This code refuses to work. I get nothing but COM errors with it when it tries to play a file.

markheath Apr 15, 2011 at 9:39 AM 
this tutorial describes MP3 playback with the latest code - won't work with 1.3. Hopefully a 1.4 release coming soon

CaptnKebec Mar 14, 2011 at 3:12 PM 
Exact same issue here, using VS 2010 and Windows 7 64 bits.

wordmountain Feb 6, 2011 at 8:03 PM 
I met excactly same issue "Only PCM supported" exception as stewbawka on the line with inputStream = new WaveChannel32(mp3Reader);
Windows 7 64 bit. VS 2008 Express edition. What's wrong?

stewbawka Feb 1, 2011 at 8:23 PM 
I get an exception "Only PCM supported" on the line with inputStream = new WaveChannel32(mp3Reader);
Am I missing something?

senfo Jan 9, 2011 at 12:19 AM 
Step 5, get rid of volumeStream and just return inputStream.