hello, i am trying to stop the music playing straight away, but instead only when the button is clicked.
any ideas?
stop();
//Stop at this frame
var music:Sound = new Sound()
//Create a new sound object
var req:URLRequest = new URLRequest("My Song 2.mp3");
//Set location of sound file
var channel:SoundChannel;
//Create a new SoundChannel object
music.load(req);
//load music file into channel object
playMusic();
var musicIsOn:Boolean = true;
//Boolean checks if the sound is playing
guitarButton.addEventListener(MouseEvent.CLICK, stopMusic);
//An event listener for a button that stops the music
function stopMusic(evt:Event):void
{
if(musicIsOn)
{
trace("stop");
channel.stop();
musicIsOn = false;
} else
{
playMusic();
musicIsOn = true;
}
}
function playMusic():void
{
trace("play");
channel = music.play();
//play music through the SoundChannel
channel.addEventListener(Event.SOUND_COMPLETE, repeat);
//add an event listener that reacts when the sound being played is completed and repeats the sound
}
function repeat(evt:Event):void
{
if(channel != null)
{
channel.removeEventListener(Event.SOUND_COMPLETE, repeat);
//without removing the event listener the sound clip will play only twice
//I am not sure why but it feels like the event listener gets disposed of
//once used by default.
playMusic();
}
}