I’m using NAudio library in a C# application. I’m trying to seek an audio (*.mp3 file) to the position I want.
However I didn’t figure out how to do it.
//Play the file starting from 16th second
waveStream.Seek(16, SeekOrigin.Begin);
And … It played starting almost from the beginning, but not from the 16th second. I also found a solution I thought true:
waveStream.Seek(waveStream.WaveFormat.AverageBytesPerSecond * 16, SeekOrigin.Begin);
It seems it’s closer the truth. Is my resolving true or not? If not what should I do?
You can set
Positiondirectly on aWaveStream, which must be converted into a byte offset – so yes, multiplying the average bytes per second by the number of seconds will get you to the right place (at least with regular PCM WAV files).WaveStreamalso has a helper property calledCurrentTimeallowing you to use aTimeSpanand it does the same calculation for you.audioFile.Position += audioFile.WaveFormat.AverageBytesPerSecond * 15;