Playing sounds with Actionscript
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…
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…’.
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.
If you paste this code onto the frame:
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.
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.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:
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.















how could u make it with sliders and knobs?
u should make part 2 with the above-
Yea I will try and do something like that.
btw pretty sweet
i never knew it was that easy
thanks a lot
check out what i made with this tutorial
http://www.swfup.com/swf-view.php?id=9097
tell me what u think
Sweet.
[…] Playing sounds with Actionscript Playing sounds with Actionscript - Awesty Productions —————— http://www.jeqq.com […]
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.
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.
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
Make the input text box have the same variable name as the dynamic text box.
How do you make it so that if you hit something it will play sound?
if(this.hitTest(something)){
sound.start(0,0);
}
What type of files do I have to save the music as?
It doesn’t really matter but flash seems to like .wav the best.
How do you make the sound stop? I tried “sound.stop” but that didn’t work
I want my bullet to create a sound when it hits the enemy, can you explain to me in full detail what to do?
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
hi, great tutorial.. mine isnt working, can you see if this code is wrong?
var water_well:Sound = new Sound();
water_well.attachSound(”water_stream1″);
water_well.onSoundComplete = function() {
water_well.start(0,0);
};
onEnterFrame = function () {
water_well.setVolume(_ymouse);
water_well.setPan(_xmouse);
};
the linkage is water_stream1… have i done it wrong?