English 中文(简体)
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

最佳回答

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.

问题回答

Try this:

In your CreateDevice method, change this line:

_playbackDevice = new WaveOut();

by this:

_playbackDevice = new WaveOut(WaveCallbackInfo.FunctionCallback()




相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签