PDA

View Full Version : Sound only plays when clicking on the pause button


inspired78
09-02-2009, 06:32 PM
Hi

All my sounds work however, the play button only seems to play my sound when I click on the pause or stop button first!
Plus, my stop button acts like a pause button, it doesn't start the song from the beginning once it stops.
Not sure why...
Any ideas?

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; // pause button
var soundIsPlaying:Boolean = true; //NEW BOOLEAN TO CHECK
mySound.load(new URLRequest("that_tune.mp3")); //loads external sound file
//mySound.play();
myChannel = mySound.play();

stop_but.addEventListener(MouseEvent.CLICK, onClickStop);
function onClickStop(e:MouseEvent):void{
myChannel.stop();
soundIsPlaying = false; //NEW BOOLEAN TO CHECK
}

play_but.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void{
if(!soundIsPlaying){ // NEW BOOLEAN TO CHECK
myChannel = mySound.play(lastPosition); // NEW BOOLEAN TO CHECK
soundIsPlaying = true; // NEW BOOLEAN TO CHECK
}
}

pause_but.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
soundIsPlaying = false; // NEW BOOLEAN TO CHECK
}

if( soundIsPlaying == true ) {
lastPosition = myChannel.position;
myChannel.stop();
}

bmyers
09-02-2009, 06:50 PM
This may not be the best way to handle the stop problem but it might work.


stop_but.addEventListener(MouseEvent.CLICK, onClickStop);
function onClickStop(e:MouseEvent):void{
myChannel.play(0); // this should reset the play position to 0 milliseconds from the start.
myChannel.stop();
soundIsPlaying = false; //NEW BOOLEAN TO CHECK
}

bmyers
09-02-2009, 06:53 PM
Even better.. In your stop function, update your lastPostion variable to 0.


lastPosition = 0;

bmyers
09-02-2009, 06:56 PM
Your first problem, "the play button only seems to play my sound when I click on the pause or stop button first!", isn't the sound already playing due to... The way it looks to me, you are playing from the get go, so the pause button or stop button would need to be pressed to actually make the play button do anything. (isSoundPlaying starts as true)


myChannel = mySound.play();