View comments | RSS feed

Pausing and resuming a sound

If your application plays long sounds, like songs or podcasts, you probably want to let users pause and resume the playback of those sounds. A sound cannot literally be paused during playback in ActionScript; it can only be stopped. However, a sound can be played starting from any point. You can record the position of the sound at the time it was stopped, and then replay the sound starting at that position later.

For example, let's say your code loads and plays a sound file like this:

var snd:Sound = new Sound(new URLRequest("bigSound.mp3"));
var channel:SoundChannel = snd.play();

While the sound plays, the SoundChannel.position property indicates the point in the sound file that is currently being played. Your application can store the position value before stopping the sound from playing, as follows:

var pausePosition:int = channel.position;
channel.stop();

To resume playing the sound, pass the previously stored position value to restart the sound from the same point it stopped at before.

channel = snd.play(pausePosition);

Flash CS3


Comments


No screen name said on Aug 14, 2007 at 10:54 AM :
This code does not work for me. I wrote

var snd:Sound = new Sound(new URLRequest("Karizma2.mp3"));
var channel:SoundChannel = snd.play();

var pausePosition:int = channel.position;

function stopSound(event:MouseEvent):void {
channel.stop();
}
audioOff_btn.addEventListener(MouseEvent.CLICK, stopSound);

function startSound(event:MouseEvent):void {
channel = snd.play(pausePosition);
}
audioOn_btn.addEventListener(MouseEvent.CLICK, startSound);

and found that it works fine BUT... the sound won't loop. If I add a position number and looping argument, the sound loops but then if I hit the start button without hitting the stop button I create what sounds like a second channel and then the stop button doesn't work. Surely there must be an easy fix.
gerry-39 said on Oct 4, 2007 at 10:29 AM :
I have the same problem.
Everytime you hit the play button, a new version of the song starts overtop of the other one???????
adbe_paul said on Oct 4, 2007 at 3:00 PM :
@gerry-39:
That's correct, it creates a new SoundChannel and plays the sound in that channel each time you call play().

To prevent that from happening, you can use the "channel" variable to keep track of whether a sound is currently playing. To do this, you could add this line of code in the stopSound() method, after the channel.stop() line:
channel = null;

And then in the startSound() method you would check whether channel is null before calling play(), by wrapping the current line in an if statement like this:
if (channel == null)
{
channel = snd.play(pausePosition);
}
jagheera nex said on Feb 21, 2008 at 1:48 AM :
i tryed everything and it still doesn't work.

heres my code(its flawless):

var snd:Sound = new Sound(new URLRequest("http://www.fileden.com/files/2007/12/1/1619416/Drowning%20Pool%20-%20Bodies.mp3"));
var channel:SoundChannel = snd.play(0, 1);
SoundMixer.soundTransform = new SoundTransform(1, 0);
StopBTN.enabled = true;

function stopSound(event:MouseEvent):void {
var pausePosition:int = channel.position;
channel.stop();
channel = null;
StopBTN.emphasized = true;
StopBTN.label = "Play";
StopBTN.useHandCursor = true;
StopBTN.addEventListener(MouseEvent.CLICK, playSound);
StopBTN.removeEventListener(MouseEvent.CLICK, stopSound);
}

function playSound(event:MouseEvent):void {
if (channel == null){
channel = snd.play(pausePosition);
}
StopBTN.label = "Stop";
StopBTN.addEventListener(MouseEvent.CLICK, stopSound);
StopBTN.removeEventListener(MouseEvent.CLICK, playSound);
}
StopBTN.addEventListener(MouseEvent.CLICK, stopSound);


whats wrong with it?
No screen name said on Mar 13, 2008 at 4:41 PM :
The problem is with the declaration of the pausePosition var, it's initial setting, and its scope.

Try this:

//Declare the pausePosition outside the function and give it no value
var pausePosition:int;

function stopSound(event:MouseEvent):void {
//NOW give the pausePosition a value (the channel.position)
pausePosition = channel.position;

//Now when you access the pausePosition in the playSound function
//it will be scoped correctly AND have the correct value. This example was
//written in a rather untested manner.

channel.stop();
channel = null;
}




Hope this helps.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000290.html