Find Out When a Sound Finishes Playing : MP3 « Development « Flash / Flex / ActionScript






Find Out When a Sound Finishes Playing

 
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.media.SoundChannel;
    
    public class Main 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 Main() {
            _playList = ["song1.mp3", 
                        "song2.mp3",
                        "song3.mp3"];
            playNextSong(  );
        }
        
        private function playNextSong(  ):void
        {
            if(_index < _playList.length) {
                _sound = new Sound(  );
                _sound.load(new URLRequest(_playList[_index]));
                _channel = _sound.play(  );

                _channel.addEventListener(Event.SOUND_COMPLETE,onComplete);

                _index++;
            }
        }
        
        public function onComplete(event:Event):void
        {
            playNextSong(  );
        }
    }    
}

                           

        








Related examples in the same category

1.Load mp3 sound file
2.Load mp3 file from a URL
3.Tracking the Progress of a Playing Sound
4.Reading the Level of a Sound
5.Calculating Spectrum Data
6.ID3 Reader