我想播放wave文件的某些部分,例如从20到50秒。我在我的C#项目中使用Naudio。我知道如何从20秒开始:
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();
但如何在50秒后停止?使用计时器对我来说不是一个好方法。
答案 0 :(得分:2)
看看OffsetSampleProvider
。它具有SkipOver
和Take
属性,允许您跳过前20秒然后接下来的30秒。
答案 1 :(得分:1)
谢谢,这很有用。代码如下:
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
...
private AudioFileReader wave = null;
private WaveOut outputSound = null;
...
wave = new NAudio.Wave.AudioFileReader(fileName);
OffsetSampleProvider offsetSampleProvider = new OffsetSampleProvider(wave);
offsetSampleProvider.SkipOver = TimeSpan.FromSeconds(timeFrom);
offsetSampleProvider.Take = TimeSpan.FromSeconds(timeTo - timeFrom);
outputSound = new WaveOut();
outputSound.Init(offsetSampleProvider);
outputSound.Play();