2013-03-18 63 views
0

我在課堂上使用Wave錄製了使用NAudio API的聲音,但是當我播放錄製的字節數組時,它給出了錯誤「波頭已損壞」。 你能否請我指出解決方案。NAudio錄製的字節數組波形標頭損壞

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

這種方法不會在所有的工作 - 錄製的音頻是不是WAV文件,你不想保持開放和多次第二閉合輸出設備。你需要做的是創建一個BufferedWaveProvider,並把你從WaveIn收到的音頻放到那裏。然後創建一個DirectSoundOutWaveOut的單個實例,並使用BufferedWaveProvider進行初始化。

+0

謝謝,幫助我很多 – Ali 2013-03-19 07:34:11