Load mp3

is there any way I can dynamically load a mp3 and have a loading bar? or preloader?

Yep. You preload MP3 exactly the same way you’d preload an swf.

can I have a preloader read the amount of the mp3 loaded if I just load the mp3 seperately and not through an swf. I am trying to load an mp3 by itself but I want to have a loading bar to display its progress.<:}

Using the values from the getBytesTotal and getBytesLoaded method you can quickly create a sound buffer, so your sound won’t start playing until enough of the sound has loaded, much better method than waiting for your sound to preload.

// create a holder for the sound, a instance of the sound object, and load in the sound file
    _root.createEmptyMovieClip("soundHolder", 1);
    userSound = new Sound(soundHolder);
    userSound.loadSound("mp3soundfile.mp3", true);
    
    // get the sound length
    soundLength = formatTime(userSound.duration)
    
    // set the textfields
    _root.min.text = soundLength.minutes;
    _root.seconds.text = soundLength.seconds;
    
    notDone == true;
    
    _root.onEnterFrame = function () {
    
        percentage = userSound.getBytesLoaded() / userSound.getBytesTotal() * 100;
        
        if (percentage > 5 && notDone == true) {
            notDone = false;
            userSound.start();
        }
        
        if (percentage >= 100) _root.onEnterFrame = undefined;
        
        percentageBar._xscale = percentage;
    
    }

function formatTime(milliseconds) {
    // find minutes expressed in decimal
    minutes = new Number((milliseconds/1000)/60);
    
    // split the minutes to find the minute (str[0]) and the seconds expressed in decimal
    str = new String(minutes.toString());
    str = str.split(".");
    
    // return an object with minutes and seconds properties
    // to find minutes, multiply the seconds expressed in decimal to find the seconds in integer format
    return {minutes:str[0], seconds: Math.round(("." + str[1])*60)};
}

h88 8]

Hey h88. Nice piece of code. But are you sure it’s safe to overwrite loadSound (especially if you use loadSound inside the function)?

Wow, how did’nt i notice that, i’ll edit the script now. :crazy:

edited