This tutorial is about streaming sound to flash externally from a website or your harddrive. Although you would think doing something like this would be very complicated and just not worth the effort, it's actually suprisingly easy.
First of all, why? Why would you want to load music externally into your flash? I'll tell you. First of all, it cuts down the filesize tremendously as you don't need to include all that music in the flash itself, and secondly it can allow the person playing the flash game or watching the animation to chose what they want to hear. With this sort of thing you could make a jukebox, media player or even a whole game based on the user finding different songs and sounds.
Ok, lets get started with the actual actionscript. In order to load sound via the URL, we use the loadSound method, which looks like this:
You must give the computer 2 bits of information in order for loadSound to work. Firstly the URL which must link to an mp3 file either on the internet or on your harddrive, and secondly whether the sound is an event or streaming sound (true for streaming, false for event). Streaming sounds play while they are downloading whilst event sounds are completely loaded before they start playing.
The first thing you must realise with loadSound is that it can only load mp3's. So if you ever find that it's not working for you, it may well be because you're trying to load a wav file. Also, the loadSound method is available for Flash 7 and higher so people with flash MX cannot use it.
To use loadSound, only 2 lines of code need to be written:
var sound:Sound = new Sound();
which demonstrates a sound being streamed, and:
var sound:Sound = new Sound();
which demonstrates a an event sound.
As you can see, in the first line of code, a variable called sound is created (it doesn't have to be called sound) and that is used to create a sound object. For more details, read AS: Sound by Denvish.
The sound will play as soon as loaded, and so whenever you want to play a sound from a URL, all that is necessary to do is type those two lines (or just the last line once you have already declared the sound variable).
Having the ability to play music from a URL isn't too great if you have to pre specify the url in the flash document. It can be very easy to allow the person viewing the flash to select which mp3 to play by having an input textbox with a var name of "songURL" and then replacing the last line of code with:
It works just the same.
Flash also allows you to find out certain things about the sound file playing. The most usefull of these are the total length of the sound file and the position of the sound file which is currently playing.
sound.duration will give you the length of the sound file in milliseconds and sound.position will give you the time in milliseconds that have played. These can be useful to display
the total time, time remaining and time gone of the sound file as shown.
trace("This sound file is " + Math.round(_root.sound.duration/1000)+" seconds" + " seconds long");
trace("This sound file has been playing for "+ Math.round(_root.sound.position/1000)+" seconds" +" seconds");
trace("There are " + Math.round((_root.sound.duration/1000)-(_r
oot.sound.position/1000)) + "seconds remaining);
I added Math.round() which will output the number as a round number and divided everything by 1000 in order to get the outputted number from milliseconds to seconds.
I hope this tutorial has been informative and that it will help people bring a larger choice of music into their flashes with this simple but effective technique.
Other things that it would be interesting to look up but i won't explain in this tutorial
(I recommend you google these or look them up on the flash help files).
getBytesLoaded - Finds the number of bytes of the sound file loaded.
getBytesTotal - Finds the total numbr of bytes of the sound file.
getPan - Gives a number from -100 (left) and 100 (right).
getVolume - Gives a number between 0 and 100.
id3 - Can give additional information on sound file (author, year created etc).
onSoundComplete - A function which is called when the sound finishes playing
setPan - Enables you to set which speaker to play the sound out of loudest. (-100-100).
setVolume - Enables you to set the volume (0-100).