我通过在类中使用Wave来使用NAudio API录制声音,但是当我播放录制的字节数组时,它会出现错误“wave header is corrupted”。 能指点我解决方案。
waveInStream = new WaveIn();
writer = new WaveFileWriter(@"C:\Users\Ali\Desktop\abc.wav", waveInStream.WaveFormat);
waveInStream.DataAvailable += new EventHandler<WaveInEventArgs(waveInStream_DataAvailable);
waveInStream.StartRecording();
void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
{
writer.WriteData(e.Buffer, 0, e.BytesRecorded);
//I want to play e.buffer but it gives me error
}
public void play_it( MemoryStream s)
{
//here i convert recorded byte into stream for playing
using (var wfr = new WaveFileReader(s))
using (WaveChannel32 wc = new WaveChannel32(wfr) { PadWithZeroes = false })
using (var audioOutput = new DirectSoundOut())
{
audioOutput.Init(wc);
audioOutput.Play();
while (audioOutput.PlaybackState != PlaybackState.Stopped)
{
Thread.Sleep(20);
}
audioOutput.Stop();
}
}
答案 0 :(得分:0)
此方法根本不起作用 - 录制的音频不是WAV文件,您不希望每秒多次打开和关闭输出设备。您需要做的是创建BufferedWaveProvider
并将您从WaveIn
收到的音频放入其中。然后创建一个DirectSoundOut
或WaveOut
的实例,并使用BufferedWaveProvider
对其进行初始化。