PDA

View Full Version : Preloading multiple mp3's using looping arrays


nawebb79
06-09-2005, 03:09 PM
I am preloading multiple mp3's dynamically using a looping array and it is working very well. However, I would like to display the percentage of the last mp3 loaded by the array. Does anyone know how I would modify the code to do this:

var my_array:Array = new Array();
my_array[0] = "Audio/audio_1.mp3";
my_array[1] = "Audio/audio_2.mp3";
my_array[2] = "Audio/audio_3.mp3";
my_array[3] = "Audio/audio_4.mp3";
count = 0;
var k;
for (k in my_array) {
var my_audio:Sound = new Sound();
my_audio.loadSound(my_array[k], true);
my_audio.setVolume(0);
var my_interval2:Number;
my_interval2 = setInterval(checkAudioProgress, 100, my_audio);
function checkAudioProgress(the_audio:Sound):Void {
var AudioKBytes = Math.ceil(the_audio.getBytesTotal()/1024);
var AudioPct:Number = Math.round(the_audio.getBytesLoaded()/the_audio.getBytesTotal()*100);
AudioPctOutput = "Loading Audio: "+AudioPct+"%"+" of "+AudioKBytes+" k";
}
count++;
}

I'm working against a deadline so any extra help would be greatly appreciated.

Many Thanks.

madgett
06-10-2005, 03:11 AM
For..in loops work backwards with arrays. So 3 is first then 2 then 1 then 0. So just loop using defined numbers. You don't want the function in the for loop because it only needs to be used once. You're code will look something like this:
var my_array:Array = new Array();
my_array[0] = "Audio/audio_1.mp3";
my_array[1] = "Audio/audio_2.mp3";
my_array[2] = "Audio/audio_3.mp3";
my_array[3] = "Audio/audio_4.mp3";
count = 0;
var k;
for (k=0; k<my_array.length; ++k) {
trace(k);
var my_audio:Sound = new Sound();
my_audio.loadSound(my_array[k], true);
my_audio.setVolume(0);
var my_interval2:Number;
my_interval2 = setInterval(checkAudioProgress, 100, my_audio);
if (k == my_array.length-1) {
checkAudioProgress(my_array[k]);
}
count++;
}
function checkAudioProgress(the_audio:Sound):Void {
var AudioKBytes = Math.ceil(the_audio.getBytesTotal()/1024);
var AudioPct:Number = Math.round(the_audio.getBytesLoaded()/the_audio.getBytesTotal()*100);
AudioPctOutput = "Loading Audio: "+AudioPct+"%"+" of "+AudioKBytes+" k";
}