0

我通过在类中使用 Wave 使用 NAudio API 录制声音,但是当我播放录制的字节数组时,它会给出错误“波头已损坏”。你能指出我的解决方案吗?

 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();
            }
        }
4

1 回答 1

0

这种方法根本行不通 - 录制的音频不是 WAV 文件,您不想每秒多次打开和关闭输出设备。您需要做的是创建一个BufferedWaveProvider并将您收到的音频WaveIn放入其中。DirectSoundOut然后创建or的单个实例WaveOut并使用BufferedWaveProvider.

于 2013-03-18T17:03:46.563 回答