Imajine Blog Imajine Articles
Contact Me

Flash 8 Actionscript tutorial to load and use 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.

Tags: Load external MP3 file into flash, Stream MP3 File, play mp3 file, Sound Object in Flash, Music play and stop tutorial in flash

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

Tutorials
Flash Tutorials
Horizontal 3D Carousel
MP3 Player
Scrapbook Photo Gallery
Text Enhancement
Color object
Glow effect using actionscript
Drawing with actionscript
Blur Effect
Load external .mp3 file
Preload external .mp3 file
Sound Controller
Set volume of externally .mp3 file
View MP3 song details
Actionscript blur effect
Actionscript fade in / out effect
Fireworks Tutorials
Custom Stroke
Typography / Ink blot effect
Photograph focus effect
Rainbow Pattern
Inset Text Effect
Fuzzy Light Blending Mode
Little bulb effect
Old paper effect
'Post it' effect
Hand drawn designs
Black/White and Sepia tone
Water color painting effect
Vintage effect on vector graphics
Solid color, black/white
Photoshop Tutorials
Photo editing
Purple Haze

Portfolio

Logos
Website design
CMS Customization
Flash
Web development
Print


Download free textures and patterns and use it for your personal use.

Click here to download Textures and Patterns.

Download free birthday cards

Free Birthday Cards
Click here to view Imajine's free downloadable birthday cards for personal and corporate use. Imajine's birthday cards are in jpeg format so it is faster to download and send in your wishes to your friends and family.
Bookmark and Share Tell a Friend jayanthi.varma@imajine.in  |  © 2008 Imajine