[英]C# NAudio - Changing audio playback position still plays a small buffer of old position
I am trying to make a basic MP3 player in C# and WPF, along with NAudio. 我正在努力用C#和WPF制作一个基本的MP3播放器,以及NAudio。 I recently added a slider that tracks the current position in the song as well as allows you to drag it to a different position and upon
Thumb.DragCompleted
it will set the song's position to where you dragged it. 我最近添加了一个滑块,可以跟踪歌曲中的当前位置,并允许您将其拖动到不同的位置。在
Thumb.DragCompleted
它会将歌曲的位置设置为您拖动它的位置。
This all works fine and all, except when I make it jump to the new position, it still plays the old position 1/5th or so of a second before changing. 这一切都很好,所有,除非我让它跳到新位置,它仍然会在更改前的1/5左右播放旧位置。 This is most noticeable eg while the singer is in the middle of singing "aaa", you pause, drag to a point where he sings "ooo", then play again, you will hear "aa-ooo".
这是最引人注目的,例如当歌手正在唱“aaa”时,你停下来,拖到他唱“ooo”的地步,然后再次播放,你会听到“aa-ooo”。 This just results in it sounding pretty horrible.
这只会让它听起来非常可怕。
So I suppose the audio playback simply has a small 'buffer' remaining that it insists on playing before moving on. 所以我认为音频播放只剩下一个小的“缓冲区”,它坚持在继续播放之前播放。 Is there any way to clear this buffer?
有没有办法清除这个缓冲区? Or am I setting the position wrong?
还是我设置错误的位置?
I am using a WaveOut waveOutDevice
and an AudioFileReader audioFileReader
. 我正在使用
WaveOut waveOutDevice
和AudioFileReader audioFileReader
。 The slider is called sldrPlaybackProgress
. 滑块称为
sldrPlaybackProgress
。
I tried to change position in the song in two different ways already: 我试图以两种不同的方式改变歌曲中的位置:
First method: 第一种方法:
audioFileReader.CurrentTime = new TimeSpan(0, (int)(Math.Floor(sldrPlaybackProgress.Value / 60)), (int)(Math.Floor(sldrPlaybackProgress.Value % 60)));
Second method: 第二种方法:
audioFileReader.Position = (long)(sldrPlaybackProgress.Value * audioFileReader.WaveFormat.AverageBytesPerSecond);
Both of these individually or even both combined still result in the problem persisting. 这些单独或甚至两者组合仍导致问题持续存在。
You should call Stop
when a reposition happens during pause. 在暂停期间重新定位时应该调用
Stop
。 This will flush any outstanding buffers and. 这将刷新任何未完成的缓冲区和。 Under the hood
Stop
calls waveOutStop
and Pause
calls waveOutPause
, which doesn't clear any in progress playing buffers. Stop
调用waveOutStop
和Pause
调用waveOutPause
,它不会清除任何正在播放的缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.