NAudio - search and navigation to play from a specified position

I am using the NAudio library in a C # application. Trying to search for an audio mp3 file in the position I want, I found that I did not understand how to do this.

//Play the file starting from 16th second waveStream.Seek(16, SeekOrigin.Begin); 

And ... He played, starting almost from the very beginning, but not from the 16th second. I also found a solution that I thought was true:

 waveStream.Seek(waveStream.WaveFormat.AverageBytesPerSecond * 16, SeekOrigin.Begin); 

It seems to be closer to the truth. Is my decision right or wrong? If not, what should I do?

+6
source share
2 answers

You can set Position directly to the WaveStream , which must be converted to a byte offset - so yes, multiplying the average bytes per second by the number of seconds, you will be taken to the right place (at least with regular PCM WAV files). WaveStream also has a helper property called CurrentTime that allows you to use TimeSpan , and it does the same calculation for you.

+7
source

If anyone else has this problem and she cannot understand. Then here is an example:

 myWaveStream.CurrentTime = myWaveStream.CurrentTime.Add(new TimeSpan(0, hours, minutes, seconds, milliseconds)); myWaveStream.CurrentTime = myWaveStream.CurrentTime.Subtract(new TimeSpan(0, hours, minutes, seconds, milliseconds)); 
+6
source

Source: https://habr.com/ru/post/914457/


All Articles