PDA

View Full Version : SOUND_COMPLETE event does not trigger


stag
06-06-2007, 12:38 PM
I am working with a short vocal track and when the audio ends I would like a replay button to appear. The codes should work fine, no errors appear. Trace displays nothing...

Here is some of the code:

var clip = "testClip.mp3";

//Create the Sound & SoundChannel Objs
var soundReq:URLRequest = new URLRequest(clip);
var sound:Sound = new Sound();

var soundControl:SoundChannel = new SoundChannel();
var resumeTime:Number = 0;

sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
soundControl.addEventListener(Event.SOUND_COMPLETE , onPlaybackComplete);
trace(sound.length);

//instants player event listners
function onComplete():void {
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);

//player_btn.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
}

//onmouseout event
function playSound(Event:MouseEvent):void {
soundControl = sound.play(resumeTime);
pause_btn.visible = true;
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.visible = false;
play_btn.removeEventListener(MouseEvent.CLICK, playSound);

}

//onmouseover event
function onMouseOut(Event:MouseEvent):void {
soundControl.stop(); }

function pauseSound(Event:MouseEvent):void {
resumeTime = soundControl.position;
soundControl.stop();
play_btn.visible = true;
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.visible = false;
pause_btn.removeEventListener(MouseEvent.CLICK, pauseSound);

}
function onPlaybackComplete(event:Event):void
{
trace("The sound has finished playing.");
}

Huinoio
06-06-2007, 12:58 PM
Is SOUND_COMPLETE an event associated with sound or soundControl?

stag
06-06-2007, 02:20 PM
Good question, i tried both with sound and soundControl:SoundChannel. Below is a link to the adobe documentation. Seems they are giving a class example instead of frame level AS.

import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;

var snd:Sound = new Sound("smallSound.mp3");
var channel:SoundChannel = snd.play();
s.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

public function onPlaybackComplete(event:Event)
{
trace("The sound has finished playing.");

http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000291.html


}

Huinoio
06-06-2007, 03:25 PM
Oh that's helpful... The s.addEventListener in Adobe document must refer to "snd". Probably a typo.
Someone left a very detailed code example you might find useful :

http://www.kirupa.com/forum/showthread.php?t=260175

WearDark
03-13-2008, 05:00 PM
Dont know if anyone will find this useful... but when you stop or pause a sound and then begin playing it again the Sound object returns a new SoundChannel object.

Therefore, everytime you get a new SoundChannel you need to addEventListener again... the previous SoundChannel object is overwritten and so you event listener disappears.

Make sense?

Chenzo
10-07-2009, 10:52 AM
Dont know if anyone will find this useful... but when you stop or pause a sound and then begin playing it again the Sound object returns a new SoundChannel object.

Therefore, everytime you get a new SoundChannel you need to addEventListener again... the previous SoundChannel object is overwritten and so you event listener disappears.

Make sense?

Holy crap!

Thanks, WearDark... this was KILLING me. I couldn't figure out why my soundCompleteHandler was not working and it was because I kept jumping to the end of the audio and getting new SoundChannels!