问题
I am using the NAudio library to write a simple WinForms audio recorder/player. My problem is how can I tell that playback is completed? I need to close the wave stream after that.
I knew there is a PlaybackStopped event listed below:
wfr = new NAudio.Wave.WaveFileReader(this.outputFilename);
audioOutput = new DirectSoundOut();
WaveChannel32 wc = new NAudio.Wave.WaveChannel32(wfr);
audioOutput.Init(wc);
audioOutput.PlaybackStopped += new EventHandler<StoppedEventArgs>(audioOutput_PlaybackStopped);
audioOutput.Play();
But this PlaybackStopped event seems can only be triggered by calling audioOutput.stop()
, does anyone know how to determine if playback is completed?
I create an open source project for this question, you can find it here: https://code.google.com/p/stack-overflow-questions/
回答1:
The PlaybackStopped
event is raised when you either manually stop playing, or the Read method of the IWaveProvider
you are using returns 0. The issue here is that WaveChannel32
does not stop returning data when it's source stream ends, so playback never ends. The PadWithZeroes
property should be set to false to fix this.
回答2:
As @Mark Heath described in addition I want to add coding example of Naudio wich will play a mp3 file in Debug/Sound Folder folder and wait until it isn't finished.
Playback is completed can be checked by waveOut.PlaybackState == PlaybackState.Stopped
play_string = @"SOUND/space.mp3";
var reader = new Mp3FileReader(play_string);
var waveOut = new WaveOut(); // or WaveOutEvent()
waveOut.Init(reader);
waveOut.Play();
while (waveOut.PlaybackState != PlaybackState.Stopped) ; // Wait untill the playing isn't finished.
来源:https://stackoverflow.com/questions/11272872/naudio-how-to-tell-playback-is-completed