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.



27 comments:
Great tutorial. How do you add more than one sound to more than one button on the same page?
@Dan and Carrie Pearce
I don't think you can add more than one sound to a button. You need to use multiple buttons.
Nice stuff... Is there any way to make the sound auto play, so we don't have to click the play button?
Thanks!
@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();
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
@Anonymous
You basically need to create a new instance of the soundID class for each sound you want to play.
Thank you very much for this tutorial. Very concise and clear.
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)
Use the stop() method to stop the main timeline. Or if you only want to stop the sound file then use: my_channel.stop();
Thanks again, also i need to ask:
How do you make the sound play again and again without stopping?
@Mark
var my_sound:SoundId = new SoundId();
var my_channel:SoundChannel = new SoundChannel();
my_channel = my_sound.play();
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
@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.
i want the sound to play when i click the button, not just play directly when i reached the frame
@xnmalex
What code are you using?
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;
}
}
@Geist,
Use the COMPLETE event.
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
@Awan
Take a look at this post
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
@Alisha
Try using this code.
SoundMixer.stopAll()
How can i apply the same method to when the up arrow key is pressed (case 38)?
@RobG,
Take a look at the Detect key press post.
im making a timer but how i can i make it so it plays a sound clip when the timer ends?
@Sergio
I believe there is a Timer complete event.
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
@Ademar,
Can you please explain I'm not sure I understand your question?
Post a Comment