|
Preloading MP3 files To preload MP3 file, we can use 'setinterval()' function to create polling mechanism that checks the bytes loaded for a Sound or NetStream object at predetermined intervals. We can use 'Sound.getBytesLoaded()' and 'Sound.getBytesTotal()' methods to track downloading progress of MP3 files. The following example uses 'setInterval()' to check the bytes loaded for a Sound object at predetermined intervals. preload an MP3 file: - Create a new FLA file called preloadMP3.fla.
- Create a text filed on the canvas, change it to 'Dynamic Text' field in the properties window
- Give an instance name 'perload_txt' to this Dynamic text field
- Select Frame 1 on the Timeline and type the following code in the Actions panel:
var songTrack:Sound = new Sound();
function checkProgress (soundObj:Object):Void { var numBytesLoaded:Number = soundObj.getBytesLoaded(); var numBytesTotal:Number = soundObj.getBytesTotal(); var numPercentLoaded:Number = Math.floor(numBytesLoaded / numBytesTotal * 100);
if (!isNaN(numPercentLoaded)) { perload_txt.text = numPercentLoaded + "% loaded."; trace(numPercentLoaded + "% loaded."); }
};
songTrack.onLoad = function ():Void { perload_txt.text = "load complete"; trace("load complete"); clearInterval(poll); };
songTrack.loadSound("http://www.imajine.in/flash/Welcome.mp3", true); var poll:Number = setInterval(checkProgress, 100, songTrack);
|
- Select Control > Test Movie to test the sound.
The Output panel shows loading progress. Click here to download the source file
Tags: Load external MP3 file into flash, Preload MP3 File, play mp3 file, Sound Object in Flash, Preload and play music, flash music preloader
|