Skip to main content
Participant
November 27, 2010
Answered

[AS3] Music Player - Play/Pause

  • November 27, 2010
  • 1 reply
  • 1639 views

At the moment, I am attempting to make a music player that will play a specific song of my choosing.

I am stuck at the part where you can pause the sound, and then play it in the same place.

I have tried many things such as using the timer utilities.

Right now, when I pause it, it stops, and when I press play, it plays from the beginning.

stop();

var channel:SoundChannel;
var soundCheck:Boolean = true;
var song:Sound = new Sound(new URLRequest("song.mp3"));
channel = song.play();

musicPlay.addEventListener(MouseEvent.CLICK, playSong);
function playSong(evt:MouseEvent):void
{
    if (!soundCheck)
    {
        channel = song.play();
    }
    soundCheck = soundCheck;
}

musicPause.addEventListener(MouseEvent.CLICK, pauseSong);
function pauseSong(evt:MouseEvent):void
{
    if (soundCheck)
    {
        channel.stop();
    }
    soundCheck = !soundCheck;
}
This topic has been closed for replies.
Correct answer

Since there is no explicit pause method - when you pause store the position property of the channel object - and then when you play, use the position to play from.

var pos:Number = 0;

play Btn:

channel = song.play(pos);

pause Btn:

pos = channel.position;

channel.stop();

1 reply

Correct answer
November 27, 2010

Since there is no explicit pause method - when you pause store the position property of the channel object - and then when you play, use the position to play from.

var pos:Number = 0;

play Btn:

channel = song.play(pos);

pause Btn:

pos = channel.position;

channel.stop();