Thursday, 20 August 2009

Playing sound files in Actionscript 3

In this tutorial you will learn how to play internal sound files in Actionscript 3.0. This is an update from the previous ‘Create a sound button’ in Actionscript 2.0. The steps in AS3 are very similar to the AS2 version, only the code will look different. Below is a comparison of the two versions.

AS2

var my_sound:Sound = new Sound();
my_sound.attachSound("sound_id");
my_sound.start();

AS3

var my_sound:SoundId = new SoundId();
var channel:SoundChannel = my_sound.play();

As you can see above the Actionscript 3.0 version uses the sound and soundChannel classes to play sounds. This is different to the previous versions of Actionscript, but will give you more control when manipulating sounds.

You will need an audio file for this tutorial. I have used a free stock sound which is available from sounddog.com. Some knowledge of creating basic buttons is also needed for this tutorial.


Playing sound files in Actionscript 3

Step 1

Open a new Flash AS3 file.
Import your audio file in the library by selecting File > Import > Import to Stage. Choose your file and then click ok.



In the library right click on your audio file and select ‘Properties’. Then select the advanced button and add the name “SoundId” for the class value, and also check Export to Actionscript and Export in frame 1. If a warning message appears click ok.


Step 2

Create a basic button on the stage which will be used to trigger the sound file. I have used two buttons from the built in button library. You can alternatively create your own buttons using the shape tools.



Give your buttons the instance names: play_btn and stop_btn respectively. If you are creating your own buttons make sure your convert them into a symbol (F8).


Step 3

On the timeline create a new layer called “actions”. Select the first frame and hit F9 to open up the actions panel and enter the following code:

//1.
var my_sound:SoundId = new SoundId();
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 which is needed to use sounds. A new instance of the soundChannel class is also created. This is needed to stop the sound as there are no methods for stopping sounds using the Sound Class.
2. This is two event listeners for the buttons with the click mouse event.
3. This function plays the sound file when the play button is clicked
4. This function stops the sound file when the stop button is clicked.


Step 4

Test your movie clip Ctrl + Enter.



You should now be able play sound files in Actionscript 3.0.

29 comments:

Dan and Carrie Pearce 28 September 2009 at 14:08  

Great tutorial. How do you add more than one sound to more than one button on the same page?

iliketo 28 September 2009 at 16:52  

@Dan and Carrie Pearce

I don't think you can add more than one sound to a button. You need to use multiple buttons.

Felix 29 September 2009 at 07:15  

Nice stuff... Is there any way to make the sound auto play, so we don't have to click the play button?

Thanks!

iliketo 29 September 2009 at 07:23  

@Felix

Yes, simply use the following code. You don't need to add the event listener.

var my_sound:SoundId = new SoundId();
var my_channel:SoundChannel = new SoundChannel();
my_channel = my_sound.play();

Anonymous 28 October 2009 at 21:45  

cheers, but how do i create multiple buttons that play different sounds, i can only seem to create multiple buttons that play te same sound

iliketo 29 October 2009 at 03:50  

@Anonymous

You basically need to create a new instance of the soundID class for each sound you want to play.

Steven 1 November 2009 at 11:54  

Thank you very much for this tutorial. Very concise and clear.

Mark 17 March 2010 at 08:21  

Thanks so much :)
Oh and i'd want to ask:
Theres a main frame, i need the sound to stop when it goes to another frame. Any help? (Oh and the other frame has sound aswell)

iliketo 17 March 2010 at 11:43  

Use the stop() method to stop the main timeline. Or if you only want to stop the sound file then use: my_channel.stop();

Mark 18 March 2010 at 06:47  

Thanks again, also i need to ask:
How do you make the sound play again and again without stopping?

iliketo 18 March 2010 at 11:24  

@Mark

var my_sound:SoundId = new SoundId();
var my_channel:SoundChannel = new SoundChannel();
my_channel = my_sound.play();

Mark 30 April 2010 at 04:29  

Hi ya was just wondering when I play a sound clip from pressing a key, if I keep pressing the key the sound keeps repaeting over itself causing mayhem!How would I get the sound to play once in full no matter if the button is pressed again and still allow the user to play it again once it has finished?
Thanks very much

iliketo 8 May 2010 at 15:07  

@Mark

You firstly need to remove the key event listener when a key is pressed. Then you need a 'sound complete' event to re-add the key event listener when the sound has finished playing.

xnmalex 24 May 2011 at 22:45  

i want the sound to play when i click the button, not just play directly when i reached the frame

iliketo 7 June 2011 at 15:27  

@xnmalex

What code are you using?

Geist 21 October 2011 at 12:07  

hello, i have an animation that loops and i have a mini player in that animation that's on autoplay. since the animation is on loop, the music starts to stack. is there a way to stop that? i want the song to finish before it starts up again.

This is my code...

import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;


//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean=false;
var myMusic:Sound=new Sound();
var soundFile:URLRequest=new URLRequest("Fluke.mp3");
myMusic.load(soundFile);
var channel:SoundChannel;

//if commands
if (AutoPlay==true) {
channel=myMusic.play();
isPlaying=true;
}


//playbutton functions
play_btn.addEventListener(MouseEvent.CLICK,playMusic);

function playMusic(event:MouseEvent):void
{ if (isPlaying== false) {
channel=myMusic.play();
isPlaying=true;

}
}

iliketo 22 October 2011 at 00:13  

@Geist,

Use the COMPLETE event.

Awan Gunawan 31 October 2011 at 09:21  

bro, is it possible if you have 2 sounds in two buttons for example btn_A & btn_B, just only one button could be played? in other words, when you click btn_A then music in btn_B automaticly stopped. could it be?
thx bro, im very new

iliketo 1 November 2011 at 02:43  

@Awan

Take a look at this post

AHHsila 24 April 2012 at 19:34  

hi!

I have a flash story book with narration, how do i stop the sound from the previous scene if the user clicks ahead before the talking is done? i haven't coded it into actions, i created a new layer on the timeline of the scene and dragged the sound file onto the stage. please help.

thanks!
-alisha

iliketo 25 April 2012 at 16:09  

@Alisha

Try using this code.

SoundMixer.stopAll()

RobG 26 April 2012 at 05:35  

How can i apply the same method to when the up arrow key is pressed (case 38)?

iliketo 26 April 2012 at 12:41  

@RobG,

Take a look at the Detect key press post.

sergio 4 May 2012 at 15:16  

im making a timer but how i can i make it so it plays a sound clip when the timer ends?

iliketo 4 May 2012 at 23:40  

@Sergio

I believe there is a Timer complete event.

Ademar 15 August 2012 at 10:14  

Thank you very much about this lesson.

I did my it and it works well.

But what about the ID tags when i'm using internal sounds (from library). I can read them?

Ademar

iliketo 15 August 2012 at 13:32  

@Ademar,

Can you please explain I'm not sure I understand your question?

Amelia Cheramu 14 July 2013 at 00:29  

hello i want to add a song in a button. i want it autoplay but when i click the button it will stop then if i click the button again the song will play again. im newbie to AS 3.0, i appriciate for u help.

iliketo 14 July 2013 at 13:40  

@Amelia,

Add a Boolean variable to detect when the button has been clicked. Then use the Boolean to check if the song is still playing. If it is playing then stop the song.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP