Search code examples
c#playbacknaudio

How to play part of wav file by Naudio/c#


I would like to play some part of wave file, e.g. from 20 to 50 second. I'm using Naudio in my C# project. I know how to start at 20 second:

private NAudio.Wave.AudioFileReader wave = null;
private NAudio.Wave.DirectSoundOut outputSound = null;
...
timeFrom = 20;
wave = new NAudio.Wave.AudioFileReader(file);
wave.Position += (long)((double)wave.WaveFormat.AverageBytesPerSecond * timeFrom);
outputSound = new NAudio.Wave.DirectSoundOut();
outputSound.Init(new NAudio.Wave.WaveChannel32(wave));
outputSound.Play();

But how to stop at 50 second? It isn't good solution for me to use timer.


Solution

  • take a look at OffsetSampleProvider. It has SkipOver and Take properties allowing you to skip over the first 20 seconds and then take the next 30 seconds.