This project has moved. For the latest updates, please go here.

Playing MP3 file fails silently

Oct 4, 2016 at 1:56 PM
Hi all, I am new to NAudio so I apologize in advance for my ignorance. I was testing around just getting an MP3 file to play and I am having a difficult time.

Here is my code:

using NAudio.Wave;
namespace ConsoleApplication2
{
class Program
{
    static void Main(string[] args)
    {
        WaveOutEvent waveOut = new WaveOutEvent();
        WaveStream mp3Reader = new Mp3FileReader("e:\\wonka.mp3");
        waveOut.Init(mp3Reader);
        waveOut.Play();
        mp3Reader.Dispose();
        waveOut.Dispose();
    }
}
}

So when I run that code nothing happens at all but I notice for a split second in the volume mixer the application appears as if it is starting to do something and then it vanishes. I have verified that I can play the file e:\wonka.mp3 in a lot of other ways.

I also had one other question while I am here. Is it possible to pass an audio "file" directly to NAudio from a byte[] rather than writing it to disk first and then passing it to the reader?
Coordinator
Oct 4, 2016 at 2:02 PM
The Play method just begins playback - it doesn't block until playback stops. So you must pause now until you want to stop or until the PlaybackStopped event is fired.
A simple solution for your app could be to sleep in a loop waiting for the PlaybackState to get set to Stopped.
And Mp3FileReader has a constructor that takes a stream so you could pass in a MemoryStream based on your byte array
Oct 4, 2016 at 2:10 PM
Is there an async/background way to play the file? Once I get this working in a "basic" way I am going to use NAudio in a WPF application and I can't pause or loop until the file is done playing. Sorry if I misunderstand.
Coordinator
Oct 4, 2016 at 2:40 PM
well in WPF make the WaveOutEvent and Mp3FileReader a member of your class. Then just call Init and Play and it will play in the background. When the PlaybackStopped event fires, you can clean up.
Oct 4, 2016 at 2:50 PM
Okay, I am still a little confused as to why the console application won't play the entire file.

I have implemented the PlaybackStopped event and it fires instantly.

What is the correct way to call waveOut.Play() so that it will continue playing until the end of the file?

By the way sorry if I am ignorant I am just following the example here:

https://github.com/naudio/NAudio/wiki/Playing-an-Audio-File

is that example current?
Oct 4, 2016 at 3:27 PM
Nevermind, I got the example working properly in a WPF application it must just be because I was doing this in a console.

thanks.
Oct 4, 2016 at 4:00 PM

The problem with your console application is that the file starts playing and then you dispose the object and exit the application before the file is finished playing.

Sid

Sid Price

Desktop Application and Embedded System Design