2013-12-13 58 views
0

我只想顯示audioOutput_PlaybackStopped中的messagebox。任何人都可以幫忙?我用Naudio。playbackstopped not raised -Naudio

這裏是我的代碼:

public void playRecordedUser() 
     { 

      pcm = new WaveChannel32(new WaveFileReader(@"D:\2.wav")); 
      //.PadWithZeroes = false; 
      reductionStream = new BlockAlignReductionStream(pcm); 
      waveOutDevice = new DirectSoundOut(); 
      waveOutDevice.PlaybackStopped += new EventHandler<StoppedEventArgs>(audioOutput_PlaybackStopped); 
      waveOutDevice.Init(reductionStream); 
      waveOutDevice.Play(); 
      // pcm.Close(); 
      // waveOutDevice.Dispose(); 
     } 

     void audioOutput_PlaybackStopped(object sender, StoppedEventArgs e) 
     { 
      MessageBox.Show("hey"); 
      waveOutDevice.Stop(); 
      reductionStream.Close(); 
      waveOutDevice.Dispose(); 
      waveOutDevice = null; 
      pcm.Close(); 
     } 
+0

這比你需要的要複雜得多。只需使用AudioFileReader。根本不要使用BlockAlignReductionStream。 –

+0

如何使用?對不起,我只是使用NAudio的新功能。感謝您的回答:) – user3098141

+0

我的問題是我不能再次用我的代碼記錄。它說@「D:\ 2.wav」正在被另一個進程使用。我只是想停止任何過程,所以我試圖使用播放停止。但仍然不會工作。 – user3098141

回答

0

的問題是,WaveChannel32繼續過去您提供的流的末尾返回數據,所以產量繼續發揮永遠。

而非目前WaveFileReader的 - >WaveChannel32 - >BlockAlignReductionStream鏈,嘗試WaveFileReader - >BlockAlignReductionStream - >Wave16ToFloatProvider這樣的:

var reduce = new BlockAlignReductionStream(new WaveFileReader(@"D:\2.wav")); 
var provider = new Wave16ToFloatProvider(reduce); 

waveOutDevice = new DirectSoundOut(); 
waveOutDevice.PlaybackStopped += new EventHandler<StoppedEventArgs>(audioOutput_PlaybackStopped); 
waveOutDevice.Init(provider); 
waveOutDevice.Play(); 

這將起到波形文件的末尾,則信號重放結束。

不要忘記在播放完成後處置reduce對象。

+0

生病請嘗試您的答案。謝謝:) – user3098141

+0

它的工作。消息框已經顯示,當我停止錄音,但問題是。在記錄我的程序後,使用waveviewer顯示波形,當我試過你的代碼時,波形顯示只是一條扁平的垂直線。當我再次嘗試錄製時,我遇到了同樣的錯誤。該@「D:\ 2.wav」正被另一個進程使用。實際上這是我想解決這個問題,爲什麼我嘗試使用播放停止。請在這件事上幫助我。我非常需要幫助。謝謝:) – user3098141

+0

不要在縮小流上調用'Close',而是調用Dispose。處理該對象肯定會關閉源流,但「關閉」可能不會。我沒有測試過。 – Corey