Monday, 24 August 2009

Play external sound file in Actionscript 3

In this Flash tutorial you will learn how to play an external sound file in Actionscript 3.0. This is an update from the previous tutorial in Actionscript 2.0. The AS3 version is very similar to the play sound file in AS3 tutorial, only one additional line of code is needed.

You need to make sure your sound file is in the same directory as your .fla or the sound file will not play. I have used free stock music from: sounddogs.com.


Play external sound file in Actionscript 3

Step 1

Open a new Flash AS3 file and create two buttons on the stage. I have used two buttons from the built in button library, but you can alternatively create your buttons using the shape tools from the tool box.




Step 2

Give your buttons the instance names: play_btn and stop_btn respectively. If you have created your own buttons make sure you convert them into a symbol first.




Step 3

On the timeline insert a new layer called “Actions”. Then select the first frame and press F9 on the keyboard to open up the Actions panel and enter the following code. Most of the code below should look familiar only one line of code is new.

//1.
var my_sound:Sound = new Sound();
my_sound.load(new URLRequest("yoursoundfile.mp3"));
var my_channel:SoundChannel = new SoundChannel();

//2.
play_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

//3.
function playSound(event:MouseEvent):void{
my_channel = my_sound.play();
}

//4.
function stopSound(event:MouseEvent):void{
my_channel.stop();
}

Code:
1. This creates a new instance of the Sound Class. The load method is then used to load the external sound file. A new instance of the Sound channel is also created.
2. This creates two event listeners for the buttons with the mouse click event.
3. This function plays the external sound file when clicked.
4. This function stop the external sound file when clicked.


Step 4

Test your external sound Ctrl + Enter.




You should now be able play external sound files in Actionscript 3.0. To play external wav file, take a look at this post.

13 comments:

sahinur 27 October 2009 05:04  

What am I doing wrong, help me pls.

**Error** Scene=Scene 1, layer=actions, frame=1:Line 3: The class or interface 'SoundChannel' could not be loaded.
var my_channel:SoundChannel=new SoundChannel();

**Error** Scene=Scene 1, layer=actions, frame=1:Line 8: The class or interface 'MouseEvent' could not be loaded.
function playSound(event:MouseEvent):void{

**Error** Scene=Scene 1, layer=actions, frame=1:Line 12: The class or interface 'MouseEvent' could not be loaded.
function stopSound(event:MouseEvent):void{

Total ActionScript Errors: 3 Reported Errors: 3

iliketo 27 October 2009 11:34  

@sahinur

Make sure you are using Actionscript 3.0.

Waqqas Akhtar 4 January 2010 06:22  

Hi!
I have the same errors.
How can I "make sure" that I'm using action script 3.0?
I'm using Adobe Flash CS4.
Regards,
Waqqas.

iliketo 4 January 2010 06:55  

@Waqqas Akhtar

This tutorial is created in Actionscript 3.0. Therefore you need to use an Actionscript 3.0 file.

If you don't know what an Actionscript 3.0 file is, then you shouldn't be using Flash.

Larry Lynch 23 January 2010 18:55  

I like this, but I would like the sound to play when the swf loads. Tips?

iliketo 24 January 2010 04:59  

@Larry Lynch

Use this.

var my_sound:SoundId = new SoundId();
my_sound.load(new URLRequest("yoursoundfile.mp3"));
var my_channel:SoundChannel = new SoundChannel();
my_channel = my_sound.play();

Hanna S 7 May 2010 14:16  

what if I want to make the button more like a switch? I know I vaguely need to adjust the visibility of each button, just not where or the exact syntax.

help?

iliketo 8 May 2010 15:02  

I created a tutorial which I think answers your question. You will need to slightly tweak the code to make it work for external sound files.

Miguel Alejandro 24 May 2010 03:20  

I get a similar error like sahinur but, and I've made sure to be scropting in AS3 this is the text of the error

Error #2044: IOErrorEvent no controlado: text=Error #2032: Error de secuencia.
at Untitled_fla::MainTimeline/frame1()
ArgumentError: Error #2068: Sonido no vĂ¡lido.
at flash.media::Sound/play()
at Untitled_fla::MainTimeline/playSound()

I copied the code as you showed and I'm still getting it thx

mael.annonay 24 December 2010 05:22  

Aaaaaaaahhhhhh!!
Thanks a looooooot!!
Since a very long time I was looking for a script which loads a music without playing it directly,
I tried to change scripts but it was always a failure, I think that it was the channel line that was missing!! ;)
Thank you again and sorry for my poor english I am not a native speaker!

iliketo 27 April 2011 08:23  

@Miguel

Try saving the fla before exporting.

kat 26 July 2011 00:42  

Hi! This is a great tutorial but I have a problem. I used this code

var my_sound:SoundId = new SoundId();
my_sound.load(new URLRequest("yoursoundfile.mp3"));
var my_channel:SoundChannel = new SoundChannel();
my_channel = my_sound.play();

to play my external sound file when the swf file loads. And it plays but the problem is, it continuously repeating playing the sound file though the first one playing haven't finish yet. Please help me.

iliketo 22 October 2011 00:22  

@Kat,

Use a boolean to check if the the sound is already playing.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP