Apr 09
Ok, starting off with an example, heres an example where it loops infinitely and you are able to pause and resume it.
The code hasn’t changed much but there are some subtle differences that have a lot of effect on the outcome.
When the pause button is pressed a new variable is set which tells the play button where to replay the sound from. Lets look at the code.
import flash.media.Sound;
var sound:Sound = new acoust();
var channel:SoundChannel;
var soundisplaying:Boolean = false;
var pausePosition:int = 0
playbut.addEventListener(MouseEvent.CLICK, playsound);
stopbut.addEventListener(MouseEvent.CLICK, stopsound);
function playsound(event:Event):void {
if (soundisplaying==false) {
channel = sound.play(pausePosition);
soundisplaying = true;
channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}
}
function stopsound(event:Event):void {
pausePosition = channel.position;
channel.stop();
soundisplaying =false;
}
function soundComplete(event:Event):void {
channel = sound.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}
}
}
function stopsound(event:Event):void {
pausePosition = channel.position;
channel.stop();
soundisplaying =false;
}
function soundComplete(event:Event):void {
channel = sound.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}
Ok, the new pause position variable is the first new thing, it is set at 0 when the movie starts, this is the start of the sound.
The next change is within the soundplay function. Where instead of the channel = sound.play(0, 99) with channel.play(pausePosition), so to recap, this is to play the sound from pauseposition, which i set earlier.
Next i add the event listener for when the sound finishes, so i can loop it. ( i can’t loop it the way i did last time as this interferes with the pause function)
The next change is the most important one, where i update the pausePosition when the stopbut button is pressed. This sets the place from which you can resume the play back when you press play.
The last thing i changed was to make a function which loops the music, it literally checks to see if the sound has finished and then replays the sound if it is.
Ok thats the basic sound tutorials over.



August 7th, 2008 at 7:20 pm
Fantastic !!
Any chance you could provide code to:
1. load an ‘external’ .mp3
2. play button code
3. your (awesome) pause button code
4. stop button code
all in one code chunk?
Loads of people are searching for the above to create simple, 3 button audio/image pieces in Flash.
Many Mahalos
bill – hawaii
August 8th, 2008 at 9:20 am
I have been moving over to a new site lately http://www.tudway-experimental.com so i will write a tutorial there for you. Post a comment on one of the tutorials there so i know you are reading them. Also i have done some experiments involving loading an external sound. If you click on the experiments tab on my new site you should see some interesting stuff.
October 30th, 2009 at 1:00 pm
Hey great tutorial.
I do have one question
how can I have the sound load and play immediately without having to invoke the play button.
what I need it music to load on my site.
then give the visitor the option to pause the music and then resume it again later.
any suggestions?
thanks
October 30th, 2009 at 1:05 pm
oh and I did find this script
that has the button functionality that I am looking for
(one button which starts/stops the audio file.
but I can’t seem to rig it into your script
(is seems like it should be easy to do???
var loadSnd:URLRequest = new URLRequest(”ax Mr .l..mp3″);
var thisSnd:Sound = new Sound();
thisSnd.load(loadSnd);
play_btn.addEventListener(MouseEvent.CLICK,playF);
stop_btn.addEventListener(MouseEvent.CLICK,stopF);
var sndTrans:SoundChannel = new SoundChannel();
var st:SoundTransform = new SoundTransform();
stop_btn.visible = false;
function playF(e:MouseEvent):void
{
SoundMixer.stopAll();
st.volume = .9;
sndTrans.soundTransform = st;
sndTrans = thisSnd.play();
play_btn.visible = false;
stop_btn.visible = true;
}
function stopF(e:MouseEvent):void
{
sndTrans.stop();
play_btn.visible = true;
stop_btn.visible = false;
}