Okay, so, here's the deal: i'm making a quiz game, and i decided to put a soundtrack to it. It is a single audio file that i uploaded to my custom domain and have it streaming through code. I also added play/pause buttons so that the player can , obviously, play and pause the music. The music autoplays on frame 1, where the "start" button of my game is. However, if the player answers a question wrong, he goes back to the first frame. The problem is that another "instance" of the music starts playing, resulting in 2 music playing on the same time. If he answers wrong again , there will be three music playback, and so on.... i need some sort of event listener that keeps this from happening... As you can see i already tried using the boolean method, but it doesn't work... Here's the code i've used for the streaming:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;// pause button
mySound.load(new URLRequest("http://trollfacequiz.16mb.com/MusicFiles/Goat%20Songs.mp3"));
var soundIsPlaying:Boolean = false;
//mySound.play();
myChannel = mySound.play();
btn_mute.pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void
{
lastPosition = myChannel.position;
myChannel.stop();
soundIsPlaying = false;
}
btn_mute.play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void
{
if (soundIsPlaying == false)
{
myChannel = mySound.play(lastPosition);
soundIsPlaying = true;
}
}
btn_mute is a movie clip symbol that contains both play and pause buttons. btn_pause is the pause button. btn_play is the play button obviously. The pause button is on top of the play button, so that when it gets invisible it "becomes" the play button...
I think the main problem is in your code structure and putting codes in frames and the shortest way to solve the problem is using SoundMixer.stopAll();
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;// pause button
mySound.load(new URLRequest("http://trollfacequiz.16mb.com/MusicFiles/Goat%20Songs.mp3"));
var soundIsPlaying:Boolean = false;
//mySound.play();
SoundMixer.stopAll();
myChannel = mySound.play();
btn_mute.pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void
{
lastPosition = myChannel.position;
myChannel.stop();
soundIsPlaying = false;
}
btn_mute.play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void
{
if (soundIsPlaying == false)
{
myChannel = mySound.play(lastPosition);
soundIsPlaying = true;
}
}