Sound in Actionscript3

You can pause and restart a sound by playing it through the channel.

The movie below lets you pause, restart and continue

  1. The mute button simply toggles between a volume of 0 and 1.
  2. The slider lets you select a volume from 0.00 to 1.00.
Get Adobe Flash player
Download movie
import flash.media.SoundChannel;

var sound:Music=new Music(); //Music is the name of the mp3 in the library
var channel:SoundChannel=new SoundChannel();
var curPos:int;
var soundPaused:Boolean=false;
channel=sound.play();
btnRestart.addEventListener(MouseEvent.CLICK,restartSong);
btnPause.addEventListener(MouseEvent.CLICK,pauseSong);
btnPlay.addEventListener(MouseEvent.CLICK,playSong);

function pauseSong(e:MouseEvent):void {
  curPos=channel.position;
  channel.stop();
}//pauseSong
function restartSong(e:MouseEvent):void {
  channel.stop(); 
  channel=sound.play();
}//restartSong
function playSong(e:MouseEvent):void {
  channel=sound.play(curPos);
}//playSong

NEXT: Project: Old MacDonald