gavin sim logo
 

Basic - week 1

Week 2

Week 3

Week 4

Week 5

Week 6

Week 7

Teaching > Flash notes

Loading and Playing Audio

Download the file electronic.mp3 and make sure you save it in the same folder you create save the fla in.

Create a new flash document.

Create a stop and a play button, give them instance names of stopBT and playBT.

Create a new layer and rename it actions. On frame 1 of this layer open the actions panel and type the following code:

var getSound:URLRequest = new URLRequest("electronic.mp3");
var sound:Sound = new Sound();
var currentSound:SoundChannel = new SoundChannel();
sound.load(getSound);

sound.addEventListener(Event.COMPLETE, loadedSound);

function loadedSound(event:Event):void
{
     playBT.addEventListener(MouseEvent.CLICK, playSound); 
     stopBT.addEventListener(MouseEvent.CLICK, stopSound); 
}

function playSound(event:Event):void
{
 currentSound = sound.play();
}


function stopSound(event:Event):void
{
 currentSound.stop();
}

The URL request is the same as we used for loading an image. The next line is creating a sound object that will enable the music to play. Then we specify a new soundChannel this enables different sounds to be player on different channels. The mp3 file is then loaded using the .load and an event listener is attached to the sound which will then call the loadedSound function when the file is fully loaded. This function then adds the listeners to the buttons, this prevents the buttons being functional before the sound is loaded.

The playSound function simply assigns the sound object to the soundChannel and calls the function play which plays the music. The stopSound function simply stops the sound playing in that particular channel.

Control / Test Movie