0
votes

I've created a on/off button for sound using Flash AS3. These work but whenever I press the off button and then the on button, the music doesn't play again?

I believe it's a looping issue but could I be wrong? I'm not sure what code to use if it is a loop problem.

I also need to add code for the btnOn function as when I open the .swf the sound automatically plays.

Attached is my current code below:

var mySound:Sound = new sandstorm(); //(sandstorm is my sound file)

var myChannel:SoundChannel = new SoundChannel();

var lastPosition:Number = 0;

myChannel = mySound.play();

btnOff.addEventListener(MouseEvent.CLICK, onClickPause);

function onClickPause(e:MouseEvent):void {

lastPosition = myChannel.position;

myChannel.stop();

}

Cheers :)

2
"...when I open the .swf the sound automatically plays" so what do you think this instruction is doing : myChannel = mySound.play();? If you want something to happen only when you click, then put that instruction inside a click handling function. PS: Is function onClickPause the one expected to both pause & resume the audio?VC.One

2 Answers

0
votes

Your code only shows the event listener onClickPause (I thinkt thats your stop button). But where is the event listener for the start / play button. On the play button you must call the play function again. Here is a great tutorial: http://www.republicofcode.com/tutorials/flash/as3sound/

0
votes

You can try the code below. It uses one button for audio pause/resume functionality...

var mySound:Sound = new sandstorm(); //(sandstorm is my sound file)
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
var audioState : String = "paused"; //will become either "playing" or "paused"


myChannel = mySound.play(); //this line starts playback
audioState = "playing"; //update because you started playback with above line

btnOff.addEventListener(MouseEvent.CLICK, onPlayPause);


function onPlayPause(e:MouseEvent):void 
{

    if (audioState == "playing") //IF already playing
    {
        lastPosition = myChannel.position; //note current "audio time" when pausing
        myChannel.stop(); //stop playback
        audioState = "paused"; //update for next time click is used
    }
    else if (audioState == "paused") //or ELSE IF was already paused then...
    {
        myChannel = mySound.play(lastPosition); //resume playback
        audioState = "playing"; //update for next time click is used
    }

}