Best viewed in Firefox

Awesty Productions

Playing sounds with Actionscript

April 24th, 2007 by awesty

In this tutorial you will learn how to play a sound from the library through actionscript and makes its volume and pan (which speaker it comes out) change. I will be using Flash 8, but Flash MX 2004 will work fine as well.

You can see an example of what we will be making HERE.

First of all you need to choose the sound you will be using. It could be a song or just a short loop. It doesn’t matter. If you click the link up you would know that mine is just a clapping noise. When you have done that click on File > Import… > Import to library…

importing the file

Now if you don’t have the library open, open it by clicking Windows > Library or Ctrl+L. You should see the sound you just imported. Right click it and click on ‘Linkage…’.

Opening the linkage window

The linkage window should now be open. Click the box that says ‘Export for actionscript’ and enter ‘mySound’ in the identifier box and click ‘OK’. Now we can refer to our sound through actionscript.


Linking the sound

If you paste this code onto the frame:

var sound:Sound = new Sound();
sound.attachSound("mySound");

…You will attach the sound from the library into a sound object in actionscript called ’sound’. If you want to refer to this sound you can just use the sound object ’sound’ (woah… i said sound 6 times in 2 sentences :O ).

Now, if you wanted your sound to play, you could add this underneath the code you just got.

sound.start(0,0);

That is using the start() function to make the sound play. The first parameter is the offset to play the sound in seconds. If you had a 60 second clip and you put 10 in this parameter flash would skip the first 10 seconds of the sound and start playing from there. This parameter is optional, so you don’t need to put it in.
The second parameter is how many times you want the sound to loop. This parameter is also optional.

If you did want your sound to loop, you could add this to your code.

sound.onSoundComplete = function(){
    sound.start(0,0);
}

The first line of that is telling flash that everytime the ’sound’ sound object stops to run the code between the curly braces. I have just put the same code as we used before to start the sound. So every time the sound finishes playing it will start again.

Now for the volume and the panning. We won’t be making any special volume slide or anything. The mouses _y coordinates will determine the volume and the mouses _x coordinates will determine the panning. Here is the code:

onEnterFrame = function(){ 
    sound.setVolume(_ymouse);
    sound.setPan(_xmouse);
}

I think that is pretty self explanatory. The first line means ‘When this frame is entered, so the code will happen every time that frame is run. The next one is setting the volume to the mouses _y coordinates, and the 3rd is setting the pan to the mouses _x coordinates. That is it for this tutorial. If you did want to stop your sound, you can use this code:

sound.stop();

You can find more tutorials here.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

18 Comments »

Comment by Timmy Subscribed to comments via email
2007-04-26 08:05:36

how could u make it with sliders and knobs?
u should make part 2 with the above-

Comment by awesty
2007-04-27 16:22:24

Yea I will try and do something like that.

 
 
Comment by Timmy Subscribed to comments via email
2007-04-26 08:06:38

btw pretty sweet
i never knew it was that easy
thanks a lot

 
Comment by Timmy Subscribed to comments via email
2007-04-26 12:48:24

check out what i made with this tutorial
http://www.swfup.com/swf-view.php?id=9097
tell me what u think

Comment by awesty
2007-07-02 19:28:25

Sweet.

 
 
2007-05-05 01:45:33

[…] Playing sounds with Actionscript Playing sounds with Actionscript - Awesty Productions —————— http://www.jeqq.com […]

 
Comment by Hannah Subscribed to comments via email
2007-05-17 00:11:58

I have a timeline with several layers which uses only 12 frames. The three main layers are:

1) “labels” (which consists of 12 different labels and takes the user to a new frame),

2) “audio” (consisting of 12 different sound/voice narrations which plays once the user clicks on an answer and goes to the next page etc),

3)”buttons” (consisting of 12 different answers that the user will click and then will automatically go to the next page).

My problem is that when I click onto an answer(button) and go to the next page the narration is overlapping and when I click again to go to another page that narration is also
overlapping with the previous one and so on & so forth. I want to use ActionScript somehow to stop the current narrations once I click an answer(button)?? In other words, as you so eloquently put if Jiru I want the previous Narrarator to ’shut up’ when another one starts talking once I’m go to the next page.

Comment by awesty
2007-07-02 19:24:29

Well the easiest way would be to add all of the tracks through actionscript, and when a new one starts just stop the previous like in the tutorial.

 
 
Comment by sam
2007-05-27 14:29:20

hey,
sorry this has like nothing to do with the tutorial =] i just found one of your tutes because i knew you could help me :)

So say i have an input text box, that you can enter your name…and then it can say
“hi [your_name] ”

oh and btw that thing timmy did with the sound was cool ;)

Comment by awesty
2007-07-02 19:30:31

Make the input text box have the same variable name as the dynamic text box.

 
 
Comment by Trunk Monkey
2007-06-01 07:22:06

How do you make it so that if you hit something it will play sound?

Comment by awesty
2007-07-02 19:27:00

if(this.hitTest(something)){
sound.start(0,0);
}

 
 
Comment by Trunk Monkey
2007-06-17 19:18:24

What type of files do I have to save the music as?

Comment by awesty
2007-07-02 19:37:34

It doesn’t really matter but flash seems to like .wav the best.

 
 
Comment by Rittto Subscribed to comments via email
2008-03-23 04:35:37

How do you make the sound stop? I tried “sound.stop” but that didn’t work

 
Comment by Jeff Subscribed to comments via email
2008-04-23 11:50:27

I want my bullet to create a sound when it hits the enemy, can you explain to me in full detail what to do?

 
Comment by prodigalG Subscribed to comments via email
2008-08-17 01:32:15

hi, i have music in the background playing through all frames, volume set to 25.
but there is a sound on frame 3 that plays fine but when i go back to frame 2 it keeps playing even though i put sound.stop();

can you help?

i tried puting it on a btn so on release start sound, and when going back on release stop sound… but that doesnt work.. something usualy goes wrong and the back m,usic stops too. HELP hehe. thanks for the tut

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.