Saturday, 14 September 2013

ActionScript 3.0 Stopping and Playing Sounds on Multiple Timelines

In ActionScript 3.0 you might have observed whilst importing sounds on Timelines (movieclips or otherwise) when you move on to the next Timeline the previous Timeline sounds do not stop.
In that case you might get 2 conflicting sounds.

For Example - you have 2 keyframes on the Timeline. 1st Keyframe has soundA movieclip containing sound_a.mp3 file and 2nd Keyframe has soundB movieclip containing sound_b.mp3 file. If you move to the 2nd Keyframe by clicking the next button and the soundA hasn’t played completely you will have a situation where soundA and soundB both are playing in the Movie.

The below code snippet would be helpful in these scenarios

// Define a soundStop() function
function soundStop()
    // Define a sndArray array and populate it with sound movieclips
    var sndArray:Array = [soundA, soundB];
    // Loop through the sndArray and stop all the sounds
    for(var i:int=0;i<sndArray.length;i++)

In 1st Keyframe write the below script
// stop all the sounds

// play the soundA snd

In 2nd Keyframe write the below script
// stop all the sounds

// play the soundB snd

Make sure to import the sound in soundA and soundB movieclips and place it in 2nd Keyframe. Give a stop() action in the 1st keyframe and set the sound behaviour to streaming in the Properties panel.
You can add as many movieclips to the array as required.

No comments:

Post a Comment