NAudio:如何获得一个事件来告诉我 MP3 文件已到达末尾?
我尝试使用这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 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.
试试这个:
在您的 CreateDevice 方法中,更改此行:
通过以下方式:
Try this:
In your CreateDevice method, change this line:
by this: