|
FLASH 8 ACTIONSCRIPTING TUTORIAL ON LOADING AND USING EXTERNAL MP3 FILES
To load external MP3 files at runtime, we need to use loadSound() method of Sound class.
This method has 2 variable as shown below,
public loadSound(url:String, isStreaming:Boolean) : Void
The isStreaming parameter indicates whether the sound is an event or a streaming sound.
Event sound - Plays only after the complete sound is loaded.
Streaming sound - Plays while downloading.
Now coming to the complete code needed to play external MP3 file -
- First and foremost, we need to create a sound object -
| var mySound:Sound = new Sound(); |
- Secondly, load external music to this new sound object created -
| mySound.loadSound("sound2.mp3", false); |
- As you can see I have set isStreaming to false, hence opting for Event sound. Upon opting event sound, I need to call the start() method of the Sound object to make the sound play. Which I have incorporated to the play button.
- To determine when a sound is completely downloaded,we will have to use Sound.onLoad event handler. This event handler automatically receives a Boolean value (true or false) that indicates whether the file is downloaded successfully
- In my tutorial I haven't used the above event handler, instead I have used a preloader. The code used for the same is given below,
_root.onEnterFrame = function() {
var downloaded = _root.mySound.getBytesLoaded();
var total = _root.mySound.getBytesTotal();
var perload = Math.round((downloaded/total)*100);
if (downloaded != total) {
_root.load_txt = "downloading song - " + perload + "%";
} else {
done = 1;
_root.load_txt = "";
}
} |
- The code I have used for play button is,
on (release) {
if (_root.done == 1) {
_root.mySound.start(0, 99);
}
} |
- and for stop button -
on (release) {
_root.mySound.stop();
} |
I hope the code was simple and clear. Do write to me with your feedback.
Download the source code.
|