Checking to see if sound sound file has finished on timeline

gwh2
gwh2 used Ask the Experts™
on
Hi everyone,

I'm using the actionscript code below to load a .mp3 file and then it checks to see when the sound is complete and then runs a function which fades another movieclip.

The code works as expected but as I need to synchronise another animation with the sound, I thought it would be easier to have the sound file on the timeline itself instead of loading it using actionscript. If I do it that way though, I still need to use actionscript to check when the sound has finished playing so that the "advertEnd" function can run.

Can someone tell me how I'd revise the code below so that it only checks to see if the sound has finished and runs the function?

Appreciate any advice.
var req:URLRequest = new URLRequest("sound.mp3");
var sound:Sound = new Sound();
var channel:SoundChannel;

function soundLoaded(event:Event):void
{
	channel = sound.play();
    if( !channel.hasEventListener( Event.SOUND_COMPLETE ) ){
            channel.addEventListener( Event.SOUND_COMPLETE, advertEnd );
       }
}

sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(req);


function advertEnd(e:Event):void
	{
		trace("FLV has ended");
		stage.addEventListener(Event.ENTER_FRAME, fadeAdvert);
	}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Commented:
hi if you're putting your sound on the timeline, you can't use code to get events from it.
i would recommend you use the code you have to load the sound rather than embedding it in the timeline.

if you absolutely insist on putting it in the timeline, the only way you can know when the sound is finished is let your keyframes extend across the number of frames of your sound, then put in a keyframe there that triggers your code.
really messy and difficult to update/maintain that.

Top Expert 2011
Commented:
Another way by using full ActionScript check SoundChannel API

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html#position

I can see you created a channel now add event listener

Now add timer for checking position

positionTimer = new Timer(50);
positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
positionTimer.start();

and handle position usecases

private function positionTimerHandler(event:TimerEvent):void {
        switch(channel.position){
             case 1000:
                  gotoAndPlay(100);
                  break;
             case 2500:
                  gotoAndPlay(200);
                  break;

      }
}
Commented:
i think the best way is adding eventDispathcer to the first and last frames.

for the first frame:
dispatchEvent(new Event("timeline_sound_started"));

and last frame:
dispatchEvent(new Event("timeline_sound_completed"));

and you can add listeners for the movieclip that holds both animation and sound.
suppose your movieclip name is animationContainer.

animationContainer.addEventListener("timeline_sound_started", soundStartHandler);
animationContainer.addEventListener("timeline_sound_completed", soundCompleteHandler);

function soundStartHandler(e:Event):void
{
trace("sound started")
}

function soundCompleteHandler(e:Event):void
{
trace("sound finished")
}



Author

Commented:
Thanks for all the replies,

Sorry I haven't responded until now as I've been really snowed under with work. I plan to test out the different options and I'll get back to you as soon as I can.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial