Loading an MP3 file

Suppose you're creating an online game that uses different sounds that depend on what level the user has reached in the game. The following code loads an MP3 file (song2.mp3) into the game_sound Sound object and plays the sound when it IS completely downloaded.

To load an MP3 file:

  1. Create a new FLA file called loadMP3.fla.
  2. Select Frame 1 on the Timeline, and then type the following code in the Actions panel:
    var game_sound:Sound = new Sound();
    game_sound.onLoad = function(success:Boolean):Void {
        if (success) {
            trace("Sound Loaded");
            game_sound.start();
        }
    };
    game_sound.loadSound("http://www.helpexamples.com/flash/sound/song2.mp3", false);
    
  3. Select Control > Test Movie to test the sound.

Flash Player supports only the MP3 sound file type for loading sound files at runtime.

For more information, see Sound.loadSound(), Sound.start(), and Sound.onLoad in the ActionScript 2.0 Language Reference. For information on preloading MP3 files, see Preloading MP3 files. For information on creating a progress bar animation when you load a sound file, see Creating a progress bar for loading MP3 files with ActionScript.

For a sample source file that loads MP3 files, jukebox.fla, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download the Samples zip file and navigate to the ComponentsAS2/Jukebox folder to access this sample. This sample demonstrates how to create a jukebox by using data types, general coding principles, and several components.


Flash CS3