Rookian
Rookian

Reputation: 20569

NAudio: How can I get an event that tells me that the MP3 file reached the end?

I tried to use this:

private void CreateDevice()
{
    _playbackDevice = new WaveOut();
    _playbackDevice.PlaybackStopped += PlaybackDevicePlaybackStopped;
}

void PlaybackDevicePlaybackStopped(object sender, EventArgs e)
{
    if (OnPlaybackStopped != null)
    {
        OnPlaybackStopped(this, e);
    }
}

But it never invoked.

Then I tried to use the PlaybackState by polling the property with a timer:

public PlaybackState PlaybackState
{
    get
    {
        if (_playbackDevice == null)
            return default(PlaybackState);

        return _playbackDevice.PlaybackState;
    }
}

But when the song ends it does not change to "stopped". But when I call manually Stop it changes correctly.

Can someone help me?

There seems to be a bug ... http://naudio.codeplex.com/WorkItem/View.aspx?WorkItemId=10726

Upvotes: 1

Views: 3050

Answers (2)

Juan Carlos Velez
Juan Carlos Velez

Reputation: 2940

Try this:

In your CreateDevice method, change this line:

_playbackDevice = new WaveOut();

by this:

_playbackDevice = new WaveOut(WaveCallbackInfo.FunctionCallback()

Upvotes: 0

Mark Heath
Mark Heath

Reputation: 49522

Because NAudio is designed to allow you to do more complicated things than simply playing one file, it will not necessarily stop at the end of a file. What determines whether WaveOut will stop is whether we stop feeding it data or not. Some WaveStreams in NAudio do stop providing data when they have reached the end of a file, but other WaveStreams will happily return buffers full of zeroes from their Read method as many times as they are called. So auto-stopping depends a lot on the graph of WaveStreams you have constructed.

Because of this you may need to determine when to stop by when you have finished reading the contents of the file to be played. I realise this is not an ideal situation, and I am still trying to come up with a design that works well both for those who just want to play a single file, and for those who are doing something a bit more involved.

Upvotes: 2

Related Questions