Search code examples
actionscript-3flashaudiosoundchannel

How can I stop music from playing multiple times when going back to the frame it starts from in as3?


I have a music track that starts on the first frame and plays throughout the entire application. When I go back to the first frame, the current music is still playing but the song starts again over the top of it. How can I make it so it only plays through once even after going back to that frame?

My code:

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();

mySound.load(new URLRequest("Stellardrone - Billions And Billions.mp3")); 
myChannel = mySound.play(); 
SoundMixer.soundTransform = new SoundTransform(0.4); 

Solution

  • You could just check to see if the sound (or sound channel) has been created yet or not:

    var mySound:Sound;
    var myChannel:SoundChannel;
    if(!mySound){
        mySound = new Sound();
        mySound.load(new URLRequest("Stellardrone - Billions And Billions.mp3")); 
        myChannel = mySound.play(); 
        SoundMixer.soundTransform = new SoundTransform(0.4); 
    }