EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 201

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Preloading external media 201
Preloading MP3 and FLV files
To preload MP3 and FLV files, you can use the
setInterval() function to create a “polling”
mechanism that checks the bytes loaded for a Sound or NetStream object at predetermined
intervals. To track the download progress of MP3 files, use the
Sound.getBytesLoaded() and
Sound.getBytesTotal() methods; to track the download progress of FLV files, use the
NetStream.bytesLoaded and NetStream.bytesTotal properties.
The following code uses
setInterval() to check the bytes loaded for a Sound or NetStream
object at predetermined intervals.
// Create a new Sound object to play the sound.
var songTrack = new Sound();
// Create the polling function that tracks download progress.
// This is the function that is "polled." It checks
// the download progress of the Sound object passed as a reference.
checkProgress = function (soundObj) {
var bytesLoaded = soundObj.getBytesLoaded();
var bytesTotal = soundObj.getBytesTotal();
var percentLoaded = Math.floor(bytesLoaded/bytesTotal * 100);
trace("%" + percentLoaded + " loaded.");
}
// When the file has finished loading, clear the interval polling.
songTrack.onLoad = function () {
clearInterval(poll);
}
// Load streaming MP3 file and start calling checkProgress()
songTrack.loadSound("beethoven.mp3", true);
var poll = setInterval(checkProgress, 1000, songTrack);
You can use this same kind of polling technique to preload external FLV files. To get the total
bytes and current number of bytes loaded for an FLV file, use the
NetStream.bytesLoaded and
NetStream.bytesTotal properties.
Another way to preload FLV files is to use the
NetStream.setBufferTime() method. This
method takes a single parameter that indicates the number of seconds of the FLV stream to
download before playback begins.
For more information, see
MovieClip.getBytesLoaded(), MovieClip.getBytesTotal(),
NetStream.bytesLoaded, NetStream.bytesTotal, NetStream.setBufferTime(),
setInterval(), Sound.getBytesLoaded(), and Sound.getBytesTotal() in Chapter 12,
ActionScript Dictionary,” on page 205.

Table of Contents

Related product manuals