Previous Page
Next Page

Recipe 15.8. Find Out When a Sound Finishes Playing

Problem

You've started playing a sound file and need to know when it has finished playing.

Solution

Listen for the soundComplete event.

Discussion

There are many cases when you might need to know when a sound is finished playing. For example, if have an audio introduction that you want to play before a user moves onto the next section of a site, you will want to know when it is safe to go to the next section. Or perhaps you have a music player with a playlist of songs. You'll want to know when each song is complete so you can start the next one.

For this recipe, and the next few, you'll be working with another class in the flash.media package, SoundChannel. When you call a Sound object's play( ) method, it returns a SoundChannel object. Thus, each sound playing in a .swf is represented by a single SoundChannel object. These channels are mixed together to produce the final audio output.

When a sound file has finished playing, its corresponding SoundChannel object fires a soundComplete event. This is defined as flash.events.Event.SOUND_COMPLETE. You can add a listener to the SoundChannel object and supply a handler method, which is called when the sound has finished playing.

The following example sets up a simple playlist:

package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.media.SoundChannel;
    
    public class PlayList extends Sprite {
        private var _sound:Sound;
        private var _channel:SoundChannel;
        private var _playList:Array;      // the list of songs
        private var _index:int = 0;       // the current song

        public function PlayList(  ) {
            // Create the playlist and start playing
            _playList = ["song1.mp3", 
                        "song2.mp3",
                        "song3.mp3"];
            playNextSong(  );
        }
        
        private function playNextSong(  ):void
        {
            // If there are still songs in the playlist
            if(_index < _playList.length) {
                // Create a new Sound object, load and play it
                // _playList[_index] contains the name and path of
                // the next song
                _sound = new Sound(  );
                _sound.load(new URLRequest(_playList[_index]));
                _channel = _sound.play(  );

                // Add the listener to the channel

                _channel.addEventListener(Event.SOUND_COMPLETE,
                                         onComplete);

                // Increase the counter
                _index++;
            }
        }
        
        public function onComplete(event:Event):void
        {
            playNextSong(  );
        }
    }    
}

Here, the index variable starts out as zero. This causes _playList[index] to be evaluated as "song.mp3", which gets loaded first. Then index is incremented. When the soundComplete event fires, the playNextSong( ) method loads the next song in _playList, until the index is greater than the number of songs in the playlist.

See Also

Recipe 15.1 for information on how to load external sound files.


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)