使用Naudio lib播放.wav文件(Play .wav files with Naudio lib)

我尝试用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 ?

I try open and play .wav files with NAudio lib.

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; }

and in play button:

waveOutDevice = new NAudio.Wave.DirectSoundOut(); reductionStream = CreateStream(openFileDialog); waveOutDevice.Init(reductionStream);

I'm using NAudio 1.5 version. For mp3 files, that play's good. When I select .wav, playing are slowly, and creaking. Maybe something wrong with WaveStream pcm = WaveChannel32?

最满意答案

您不需要使用BlockAlignReductionStream,WaveChannel32或CreatePcmStream。 只需使用Mp3FileReader或WaveFileReader并将其传递给您的IWavePlayer,

You don't need to use BlockAlignReductionStream, WaveChannel32 or CreatePcmStream. Just use the Mp3FileReader or WaveFileReader and pass that in to your IWavePlayer,

更多推荐