使用Naudio lib播放.wav文件
本文关键字:wav 文件 播放 lib Naudio 使用 | 更新日期: 2023-09-27 18:29:50
我尝试用NAudio-lib打开并播放.wav
文件。
private OpenFileDialog openFileDialog = null;
private NAudio.Wave.IWavePlayer waveOutDevice;
private NAudio.Wave.BlockAlignReductionStream reductionStream = null;
private NAudio.Wave.BlockAlignReductionStream CreateStream(OpenFileDialog fileDialog)
{
if (fileDialog.FileName.EndsWith(".mp3"))
{
NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(fileDialog.FileName));
reductionStream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else if (fileDialog.FileName.EndsWith(".wav"))
{
NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(openFileDialog.FileName));
reductionStream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else
{
throw new InvalidOperationException("Unsupported");
}
return reductionStream;
}
和播放中按钮:
waveOutDevice = new NAudio.Wave.DirectSoundOut();
reductionStream = CreateStream(openFileDialog);
waveOutDevice.Init(reductionStream);
我使用的是NAudio 1.5版本。对于mp3文件来说,播放效果很好。当我选择.wav
时,播放缓慢,吱吱作响。WaveStream pcm = WaveChannel32
可能有问题?
您不需要使用BlockAlignReductionStream、WaveChannel32或CreatePcmStream。只需使用Mp3FileReader或WaveFileReader,并将其传递给您的IWavePlayer、