[Tutorial] Flash - Handling music by code

Come here to View/Post any tutorials here.
x2i said
Post Fri Jul 11, 2008 1:21 am
Any n00b that uses flash can figure out by themselves how to make a sound play on a certain frame - however this gives us a complete lack of control on how we want the sound to play (besides looping and panning).

Attaching Sound Files

What if we wanted a sound to play upon a certain event - even if the frame hasnt changed. Well we can call upon sounds from the library providing they have a linkage.

So import a sound file into flash and give the library element a linkage name of 'soundfile'. Now on the first frame write this:

Code: Select all
s = new Sound();
s.attachSound("soundfile");


This declares that we want to make a new sound that plays our sound. Now use this to trigger the sound whenever you like:

Code: Select all
_root.s.start();


Linking to external Sound Files

We can also link to a sound file outside of the flash movie using the following method:

Code: Select all
mySound = new Sound();
mySound.onLoad = function(success) {
   if (success == true) {
      mySound.start();
   }
};
mySound.loadSound("mySoundFile.wav");


This will locate your sound file from whichever path you specified and then when it has fully loaded it - it will start to play.

To stop all sounds at any time simply use:

Code: Select all
stopAllSounds();
Signature

x2i said
Post Fri Jul 11, 2008 1:22 am
We can set the volume and panning of those sounds as well, using these two codes

Controlling volume
Code: Select all
mySound = new Sound();
_root.attachSound("mySoundFile");
mySound.setVolume(50);


Controlling panning
Code: Select all
mySound = new Sound();
_root.attachSound("mySoundFile");
mySound.setPan(50);
Signature

x2i said
Post Fri Jul 11, 2008 1:22 am
The following code will return the percentage of what position we are in the music file. For example when the the sound is finished it will display 100% but when it is only half way through it will display only 50%.

First place a dynamic textbox on the stage and call it 'display'.

Import a sound file and in the library panel, give it a linkage name of "sound".

Then paste this code in the first frame:
Code: Select all
mySound = new Sound();
mySound.attachSound("sound");
mySound.start();
onEnterFrame = function () {
   var percent = (mySound.position / mySound.duration) * 100;
   _root.display = Math.floor(percent) + "%";
};
Signature

x2i said
Post Fri Jul 11, 2008 1:23 am
If you would like to detect when a sound has finished playing, you can use this:

Code: Select all
mySound.onSoundComplete = function() {
      //Code Here
};


This could be used to loop a sound also...
Signature

[TCRN] said
Post Sun Sep 06, 2009 3:35 pm
Hey x2i just letting you know theres an alternate way to stop it:

Code: Select all
on(release)
{
      mySound.stop();
}


This way you can stop one or multiple sounds at a time.
Signature

[ Sort Options ]

Return to Tutorials




Who is online?

Users browsing this forum: No registered users and 1 guest

cron