![MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT Reference Manual Download Page 201](http://html1.mh-extra.com/html/macromedia/flash-mx-2004-actionscript/flash-mx-2004-actionscript_reference-manual_3378897201.webp)
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("%" + percent " 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
.
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...