Class Variables in ActionScript3

Get Adobe Flash player

In order to stop and restart a sound you need to play it through a channel. In order to notice, you need a fairly long sound.
In this example we used Sports Basketball Dribble and Shoot in Gym 01.

  1. Start a new flash movie.
  2. Copy a sound from the common sounds library to the library for the movie or import a long sound.
  3. In the library for the movie, select the sound, then right click and select properties from the list.
  4. Name the sound BasketBall:
    Name sound BasketBall
  5. Add three buttons named btnBack, btnPause, and btnPlay.
  6. Add text named txtPos.

Write the code as shown below:

var sound:BasketBall=new BasketBall();  //get a fairly long sound in the library or off the internet
var channel:SoundChannel;
var curPos: int=0;
var playing:Boolean=false;
btnBack.addEventListener(MouseEvent.CLICK,goBack);
btnPause.addEventListener(MouseEvent.CLICK,goPause);
btnPlay.addEventListener(MouseEvent.CLICK,goPlay);
this.addEventListener(Event.ENTER_FRAME,frames);
function frames(e:Event):void {
if(playing) {
curPos=channel.position;
}
txtPos.text=""+curPos;
}
function goPlay(e:MouseEvent):void {
channel = sound.play(curPos);
playing=true;
}
function goBack(e:MouseEvent):void {
if(playing) {
channel.stop();
}
playing=false;
curPos=0;
}
function goPause(e:MouseEvent):void {
if(playing) {
curPos=channel.position;
channel.stop();
playing=false;
} //stop playing
} //go pause

Download the finished movie

NEXT: URLRequest to Open the Browser