NAudio:如何获得一个事件来告诉我 MP3 文件已到达末尾?

发布于 2024-08-27 05:39:40 字数 860 浏览 7 评论 0原文

我尝试使用这个:

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

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

但它从未调用过。

然后我尝试通过使用计时器轮询属性来使用 PlaybackState:

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

        return _playbackDevice.PlaybackState;
    }
}

但是当歌曲结束时,它不会更改为“已停止”。但是当我手动调用 Stop 时,它会正确更改。

有人可以帮助我吗?

似乎有一个错误... http://naudio.codeplex.com /WorkItem/View.aspx?WorkItemId=10726

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-09-03 05:39:40

因为 NAudio 的设计目的是让您可以做比简单播放一个文件更复杂的事情,所以它不一定会停止在文件末尾。决定 WaveOut 是否停止的是我们是否停止向它提供数据。 NAudio 中的某些 WaveStream 在到达文件末尾时确实会停止提供数据,但其他 WaveStream 会很乐意从其 Read 方法中返回充满零的缓冲区,次数与调用次数相同。因此自动停止在很大程度上取决于您构建的 WaveStreams 图表。

因此,您可能需要确定在读取完要播放的文件的内容后何时停下来。我意识到这不是一个理想的情况,并且我仍在尝试提出一种设计,该设计既适合那些只想播放单个文件的人,也适合那些正在做一些更复杂的事情的人。

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.

一个人练习一个人 2024-09-03 05:39:40

试试这个:

在您的 CreateDevice 方法中,更改此行:

_playbackDevice = new WaveOut();

通过以下方式:

_playbackDevice = new WaveOut(WaveCallbackInfo.FunctionCallback()

Try this:

In your CreateDevice method, change this line:

_playbackDevice = new WaveOut();

by this:

_playbackDevice = new WaveOut(WaveCallbackInfo.FunctionCallback()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文