Music Looping in AS3

According to the AS3 documentation you can loop a sound by passing in a loop number argument when you call play on a sound. Apparently, that never seems to work consistently for me. Sometimes it would not loop after a number of times, or it doesn’t loop immediately. So I usually just manually control the looping of music with Event Listeners.

To do this, I add an Event Listener to the music channel that will call a function to replay the sound whenever it’s done playing once.

public function playMusic():void
{
    musicChannel = music.play();
    musicChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);
}

public function loopMusic(e:Event):void
{
    if (musicChannel != null)
    {
        musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopBackgroundMusic);
	playMusic();
    }
}

So the sound will continue to loop forever until I stop it. Whenever I do, I have to remove the Event Listener from the music channel or else it won’t be garbage collected.

public static function stopMusic():void
{
    if (musicChannel != null)
    {
	musicChannel.stop();
	musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopMusic);
    }
}

That’s about it. A pretty simple solution to an annoying problem sometimes.

Tags: ,

15 Responses to “Music Looping in AS3”

  1. Jadon says:

    cool thanks, although i think:

    musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopBackgroundMusic);

    should be:

    musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopMusic);

  2. Ruth says:

    i’m sry, but wat is that musicChannel != null checking?

  3. … @ a distance … » Actionscript says:

    [...] or twice. Again I went around reading on codes.. And found I wasn’t alone. Then I came across a simpler workaround using functions call, better than the former [...]

  4. David B says:

    I’ve used the the same methods as thise described above several times. A simpler way to achieve the same thing is to use the following:

    musicChannel = music.play(0, int.MAX_VALUE);

    This basically takes the maxium integer value available in AS3 as the second param. It should be sufficient enough for most use cases.

  5. ZennMaster says:

    Thanks man, i’ve been searchin for something like this simple musicChannel = music.play(0, int.MAX_VALUE);

  6. Tom says:

    I think you can get around having to remove the listeners every time by using weak references, detailed here:

    http://www.gskinner.com/blog/archives/2006/07/as3_weakly_refe.html

    I’ve not tried it, but it sounds like what you’re after – weak references don’t stop something being garbage collected.

    Loved Zunderfury, by the way!

  7. Graeme says:

    @Ruth the != null is there to prevent an exception being thrown if you try to delete something that is null already

  8. Looping sounds in ActionScript 3 | The Devign Path says:

    [...] To loop a sound clip in Flash using ActionScript 3 refer to – http://doogog.com/music-looping-in-as3.html [...]

  9. Alex says:

    Hey, I used a really simple method to loop sound.

    function playSound(event:Event):void
    {
    myChannel = mySound.play();
    myChannel.addEventListener(Event.SOUND_COMPLETE, playSound);
    }

    As for only looping it a certain amount of times…you could just keep duplicating this function but changing the names for however many times you want it to loop. (playSound1, playSound2, playSound3, etc…) Then just redirect each function to the next one on SOUND_COMPLETE.

  10. Peter says:

    Thanks a lot for this solution :-) Love the way and now always use it to control my music!

  11. Sean says:

    Hey I like your solution Alex!

  12. Sold Out Activist says:

    @alex It’s a good idea, but you forgot to remove the event listener before adding it again. Each playback will add another event listener. After 3 loops, it will have 6 event listeners attached to it, growing exponentially.

  13. nconde25 says:

    @peter:
    or you could use a FOR?

  14. Ethian says:

    Hi i’m new to as3, i try the code, there’s no error but my music just won’t loop…did i miss something that’s not written here?

  15. ifirme.ro says:

    if you add a listener, use it !

    function loopMusic(e:Event):void
    {
    soundChannel.removeEventListener(Event.SOUND_COMPLETE, loopMusic);
    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);

    }

Leave a Reply