Blocking Program Execution When Playing MP3/WAV with WMPLib


i have application thread runs in background.  that thread watching new data and, when data received, processed - includes playing 2 sounds.

each play sound function [ e.g. play1() , play2() ], each of set follows:

wmplib.windowsmediaplayer wplayer = new wmplib.windowsmediaplayer();  wplayer.url = "c:\\dir\\sound.mp3"; // play1() mp3, play2() wav wplayer.controls.play();

however, when data processing happens, play1() fires off play2() fires , 2 sounds play on top of 1 another.

i play sound1, play sound2, continue threaded function looking new data.

how can pause execution of application while audio playing?  i found information suggesting use of timer, not familiar using them , attempts unsuccessful.

edit:
i guess better way of thinking might pretend want pop message box after the audio file has finished , don't want return play1() function until after file has finished , message box has been shown.  e.g: 

wmplib.windowsmediaplayer wplayer = new wmplib.windowsmediaplayer();  wplayer.url = "c:\\dir\\sound.wav";  wplayer.controls.play(); messagebox.show("done."); return;

however code doesn't work way.  when function called, message box, audio start, , return happen @ approximately same time.  how can hang program (or otherwise make wait) until playback finished?


i have found solution appears work current needs in stack overflow thread titled "soundplayer.playsync stopping prematurely" in reply user jeffe.

this solution turns play1() , play2() function blocking functions yielding thread until playstate out of transitioning state.  then, duration accessible, can yield thread again , use blocking thread.sleep() call duration of audio file.

wplayer.url = "c:\\dir\\audio.mp3"; wplayer.controls.play(); thread.yield(); while (wplayer.playstate == wmplib.wmpplaystate.wmppstransitioning) { 	application.doevents(); 	thread.yield(); } int duration = convert.toint32(wplayer.currentmedia.duration * 1000); thread.sleep(duration);




Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'