PDA

View Full Version : How to detect when a playing audio file has reached the end (stops playing)?



musicman1088
August 31st, 2009, 05:42 PM
I have some animation (programmatic, I'm NOT using keyframed animation in timeline), and some music I want to play in the background. Once the music file reaches the end (about 30 seconds long), I want to trigger the end of the animation functions.

I figure I could either use an actionscript timer to trigger the end of the animation or a keyframe with actionscript that is placed on the timeline near the end of the song but these methods assume knowledge about the duration of the wave file. If I changed the framerate or updated the external wave file, these triggers would possibly no longer sync up.

Is there some sort of event listener or other detection method for an audio file to signal it has finished playing? Or maybe there is a way to get the duration of the audio file to use in the timer? Thanks in advance.

IQAndreas
August 31st, 2009, 06:11 PM
How are you loading your sound file?

When you play a sound using the "play()" function, it returns a SoundChannel object.
Listen to this object for a "SOUND_COMPLETE" event.


var mySound:Sound = getSound();

var mySoundChannel:SoundChannel = mySound.play();
mySoundChannel.addEventListener(Event.SOUND_COMPLE TE, songFinished);

function songFinished(ev:Event):void
{
trace("The Song is done!");
this.gotoAndStop(60);
}

musicman1088
September 1st, 2009, 04:02 PM
Thanks for the reply, just what I was looking for.... Although as I play with it more, it occurs to me that I should be triggering the transitional animation a little earlier than when the song finishes playing. I suppose I need to resort to timers for that huh? Unless there is a way to play place cue markers inside the audio file to trigger/sync the animation?

I guess the question should have been, how do I sync animation with arbitrary song events, (chorus, bridge, solo) of a song? I'm thinking laying the waveform out on a layer in the timeline, and placing actionscript triggers in the individual frames is still the best way to go on this one...

IQAndreas
September 1st, 2009, 04:59 PM
I'm guessing that the delay is caused by that little, quiet, "whitespace" at the end of the song. Most songs have a second or two so they don't immediately jump into the next song.

This might take a bit of coding, but you could do you said and create your own "cue point" dispatcher. Hold all possible cue points in an array, and when the song is playing, keep checking if the song has passed one of the points. If so, dispatch a custom cue event.

Or you could just keep checking when the song is playing when the song is two seconds away from the end or so. That might work as well.