你解决了这个问题吗?根据您的评论,如果您只需要更改 sampleRate,那么您为什么要使用 NAudio ?您可以使用默认可用的播放器,例如 MediaPlayer/SoundPlayer。如果是这样,您可以参考以下代码。我添加了一种更改采样率的方法。尽管您可以单独编写或附加 waveFormat,但我只提到了采样率及其相关字段。我正在阅读整个文件,关闭然后打开它以逐部分写入。
(C# 中“WaveHeader 格式”的原始参考:http: //www.codeproject.com/Articles/15187/Concatenating-Wave-Files-Using-C-2005)
public void changeSampleRate(string waveFile, int sampleRate)
{
if (waveFile == null)
{
return;
}
/* you can add additional input validation code here */
/* open for reading */
FileStream fs = new FileStream(waveFile, FileMode.Open, FileAccess.Read);
/* get the channel and bits per sample value -> required for calculation */
BinaryReader br = new BinaryReader(fs);
int length = (int)fs.Length - 8;
fs.Position = 22;
short channels = br.ReadInt16();
fs.Position = 34;
short BitsPerSample = br.ReadInt16();
byte[] arrfile = new byte[fs.Length];
fs.Position = 0;
fs.Read(arrfile, 0, arrfile.Length); /* read entire file */
br.Close();
fs.Close();
/* now open for writing */
fs = new FileStream(waveFile, FileMode.Open, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.BaseStream.Seek(0, SeekOrigin.Begin);
bw.Write(arrfile, 0, 24); //no change till this point
/* refer to waveFormat header */
bw.Write(sampleRate);
bw.Write((int)(sampleRate * ((BitsPerSample * channels) / 8)));
bw.Write((short)((BitsPerSample * channels) / 8));
/* you can keep the same data from here */
bw.Write(arrfile, 34, arrfile.Length - 34);
bw.Close();
fs.Close();
}
现在您可以调用上述方法并播放不同采样率的波形文件:
changeSampleRate(yourWaveFileToPlay, requiredSampleRate);
MediaPlayer mp = new MediaPlayer();
mp.Open(new Uri(yourWaveFileToPlay, UriKind.Absolute));
mp.Play();