Loading External Media

I l @ ve RuBoard

An alternative to streaming one large movie is to create a movie that isn't full of media but loads that media from external files as it is needed. You can build a large presentation that is spread out over several files.

Replacing the Current Movie

The simplest way to do this is to divide the movie into several parts . When one movie ends, you can go to another. All you need to do is issue the loadMovie command.

For instance, you can have a frame at the end of a long animation where the movie stops. When the user is ready, he can click a button to go to the next animation, which is in another movie:

 on (release) {     loadMovie("animation2.swf"); } 

Or, you could give the user a choice. At the end of a movie there could be two buttons . The user chooses which animation to see next. In a situation where the user makes a choice like this, keeping content in separate movies is a great choice. After all, the user will probably never see the other content, so why require her to load it over the Internet?

Check out the sample movie 23movie1.fla. When you test it, there are buttons that take you to one of two separate movies. Then the user can go back to the original movie. This is a good but simple example of how you can divide your presentation over several files rather than just one.

Loading a Movie Clip

The loadMovie command can also be used to load a movie into a movie clip. Instead of changing movies completely, you can just swap the content in a movie clip.

All you need to do is specify the movie clip that is to be replaced by the external file. For instance, to replace myMovieClip with the file othermovie.swf, just do this:

 myMovieClip.loadMovie("othermovie.swf"); 

When using loadMovie , you can use the getBytesTotal and getBytesLoaded functions to check the progress of the load. This means that you can report the progress to the user with text or even a progress bar like the one used earlier in this hour .

If you want to preload a movie clip to have it ready before the user even gets to a place where it is needed, you can set up the external movie so that nothing is on the first frame but a stop() command. Then load the movie into a blank movie clip that is off the stage. When it completes, the movie just sits there on its blank first frame.

However, the movie file will be sitting in the user's browser cache. Now when it comes time to really use the movie clip, do the loadMovie command again. The movie will be there quickly because the file has already been downloaded. Then issue a gotoAndPlay(2) command to get it past that first frame.

Loading a JPEG

Flash MX has an new capability that developers have been begging Macromedia for since they bought Flash; it can import an external JPEG file.

The way you do this is simple. Just use the a loadMovie command like you are planning to replace a movie clip with an external Flash movie. However, give the location of a JPEG file instead:

 myMovieClip.loadMovie("picture.jpg"); 

The movie clip is now replaced by a movie clip that holds that bitmap image. Check out the movie 23loadjpeg.fla for an example.

Loading a Sound

There are also two ways to play a sound from an external file. They both use the sound object and the loadSound command. The sound file needs to be in the popular MP3 format.

Here is an example of the first method, which plays an event sound. This means that the entire sound is loaded into memory first and then played if a start command is issued.

 on (release) {     mySound = new sound();     mySound.loadSound("mysound.mp3",false);     mySound.start(); } 

Flash remembers that the start command was issued, even though the sound has just begun to download. When the sound is finished downloading, it plays immediately.

The second method uses a true in the second parameter. This tells Flash to stream the sound in. As soon as some of the sound has been loaded, the sound starts playing. The sound continues as the rest is loaded. If the user's connection is a good one, the user will hear the entire sound as it loads.

 on (release) {     mySound = new sound();     mySound.loadSound("mysound.mp3",true); } 

Notice that you don't need the start command with a streaming sound. However, you do need to pay careful attention to how you make the MP3 file. For instance, if you use the 128Kbps or 160Kbps setting that is popular for playing MP3 music, it will be too large of a file to stream over the Internet, especially if the user has a modem. 32Kbps or less will give the stream a chance to work successfully.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net