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:

  1. Create a new FLA file called preloadMP3.fla.
  2. Create a text filed on the canvas, change it to 'Dynamic Text' field in the properties window
  3. Give an instance name 'perload_txt' to this Dynamic text field
  4. 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);


  5. 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

Download free Photo Gallery Download Free Photo Gallery

You can download Imajine's Scrapbook Photo Gallery absolutely free of cost and to customize , you need no knowledge of Adobe Flash.  Click here to view Scrapbook Photo Gallery / download

Bookmark and Share

ABOUT IMAJINE

Team Imajine is a good mix of design and code –with an eye for detail and strong belief in accessibility and usability. We are a web design and development boutique firm focusing on generating a strong online presence and visibility for our customers.

Apart from web design, logo design, content management systems, print and web development services, Imajine’s creative portfolio extends to photography, game programming and tutorial development (CSS3, HTML5, illustrator, Adobe Fireworks, Adobe Photoshop and Adobe Flash).

Leveraging on latest web technologies, we at Imajine adopt a progressive approach to design, develop and innovate affordable solutions with increased returns and enhanced business perceptions.

Looking for affordable web solutions? Contact Imajine to initiate a discussion on your web requirements.

jayanthi.varma@imajine.in  |  © 2008 - 2012 Imajine.