Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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. This is most noticeable e.g. 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". 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. The slider is called 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.

like image 695
stevenJ Avatar asked Aug 19 '15 16:08

stevenJ


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You should call Stop when a reposition happens during pause. 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.

like image 134
Mark Heath Avatar answered Sep 30 '22 01:09

Mark Heath