Connecting with External Sources > Sending and loading variables to and from a remote source > Loading an image or sound dynamically

 

Loading an image or sound dynamically

If you import an image or a sound while you author a Flash document, the image and sound are then packaged and stored in the SWF file when you publish the movie. To load JPEG images at runtime, you use the loadMovie or loadMovieNum method of the MovieClip object. To load MP3 sounds at runtime, you use the loadSound method of the Sound object. To return the number of bytes that have downloaded and the expected number of bytes for the image or sound file being downloaded, you use the getBytesLoaded and getBytesTotal methods of the MovieClip and Sound objects.

To load an image into a level in the Flash Player, you must use the loadMovieNum method or action. To load an image into a movie clip target in the Flash Player, you must use the loadMovie action or method. The loaded image replaces all the contents of the target movie clip.

To load a sound, you must create a new instance of the Sound object. You can use the new instance to call the loadSound method to load an event or a streaming sound. Event sounds are loaded completely before being played; streaming sounds are played as they are downloaded. You can set the isStreaming parameter of the loadSound method to specify a sound as an event sound or a streaming sound. After you load an event sound, you must call the start method of the Sound object to make the sound play. Streaming sounds begin playing when sufficient data is loaded into the movie; you don't need to use the start method.

Note: For image files, Flash supports only the standard JPEG image file type, not progressive JPEG files. For sound files, Flash supports only the MP3 sound file type.

 
To load an image dynamically:

1

Choose a frame, button, or movie clip to which to assign the action.

2

Choose Window > Actions to open the Actions panel if it isn't already open.

3

In the Actions toolbox, click the Objects category, click Movie, MovieClip, and Methods, and double-click the loadMovie method to add it to the Script pane.

4

In the Object parameter box, enter the instance name of the movie clip into which the image will load—in this example, myMC.

If the movie clip is not a child of the same parent as the Timeline that calls the action, you must use a target path. You can use an absolute or relative path.

5

In the Parameters box, enter the URL at which the image is located. Enter a comma (,) after the URL.

6

After the comma in the Parameters box, enter the HTTP method "GET" or "POST" (in quotation marks), or leave the parameter blank.

For example, the following code loads an image into a movie clip on the Timeline where the movie clip that calls the action is located.

myMC.loadMovie("http://www.foo.com/ImagesToLoad/image1.jpg")

 
To load a sound dynamically:

1

Choose a frame, button, or movie clip to which to assign the action.

2

Choose Window > Actions to open the Actions panel if it isn't already open.

3

In the Actions toolbox, click the Actions category, click Variables, and double-click the set variable action to add it to the Script pane.

4

In the Variable parameter box, enter an instance name for the new object, for example, mySound.

5

With the insertion point in the Value parameter box, from the Actions toolbox, click the Objects category, click Movie, click Sound, and double-click new Sound to add it to the Script pane. Select the Expression box.

The code should look like this:

mySound = new Sound();

6

In the Actions toolbox, click the Objects category, click Movie, Sound, and Methods, and double-click the loadSound method to add it to the Script pane.

7

In the Object parameter box, enter the instance name of the movie clip into which the sound will load—in this example, mySound.

8

In the Parameters box, enter the URL at which the sound is located. Enter a comma (,) after the URL.

The URL must be enclosed in quotation marks, for example, "http://www.foo.com/SoundsToLoad/sound14.mp3".

9

After the comma in the Parameters box, enter the value false for the isStreaming parameter to indicate that the sound is an event.

For example, the following code loads an event sound:

this.loadSound("http://www.foo.com/SoundsToLoad/sound14.mp3", false)

10

In the Actions toolbox, click the Objects category, click Movie, Sound, and Methods, and double-click the start method to add it to the Script pane.

11

In the Object parameter box, enter the instance name of the sound to start—in this example, mySound.

The code should look like this:

mySound = new Sound();
mySound.loadSound("http://www.foo.com/SoundsToLoad/sound14.mp3", true);
mySound.start();

For more information, see MovieClip (object) and Sound (object) in the ActionScript Dictionary.