2017-01-12 80 views
0

我想播放wave文件的某些部分,例如從20秒到50秒。我在我的C#項目中使用Naudio。我知道如何在20秒開始:如何播放Naudio/c的wav文件的一部分#

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秒?對我來說,使用計時器並不是一個好的解決方案。

回答

1

看看OffsetSampleProvider。它具有SkipOverTake屬性,允許您跳過前20秒,然後再跳過30秒。

0

謝謝你,這很有用。代碼如下:

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();