gpt4 book ai didi

c# - 使用 Naudio 库播放 .wav 文件

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:18 25 4
gpt4 key购买 nike

我尝试用 NAudio 库打开并播放 .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,

关于c# - 使用 Naudio 库播放 .wav 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9979972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com