Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naudio,how to tell playback is completed

Tags:

c#

naudio

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/

like image 725
Steven Du Avatar asked Jun 30 '12 09:06

Steven Du


2 Answers

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.

like image 132
Mark Heath Avatar answered Oct 03 '22 07:10

Mark Heath


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. 
like image 43
Muhammad Ashikuzzaman Avatar answered Oct 03 '22 07:10

Muhammad Ashikuzzaman