Example The following example illustrates the use of many of the MovieClipLoader class methods and listeners: // first set of listeners var my_mcl:MovieClipLoader = new MovieClipLoader;
Trang 1MovieClipLoader class 601
Listener summary for the MovieClipLoader class
Constructor for the MovieClipLoader class
MovieClipLoader.onLoadComplete Invoked when a file loaded with MovieClipLoader.loadClip()
has completely downloaded.
MovieClipLoader.onLoadError Invoked when a file loaded with MovieClipLoader.loadClip()
has failed to load.
MovieClipLoader.onLoadInit Invoked when the actions on the first frame of the loaded clip
have been executed.
MovieClipLoader.onLoadProgress Invoked every time the loading content is written to disk during
the loading process.
MovieClipLoader.onLoadStart Invoked when a call to MovieClipLoader.loadClip() has
successfully begun to download a file.
Trang 2The following example loads an image into a movie clip called image_mc The movie clip instance
is rotated and centered on the Stage, and both the Stage and movie clip have a stroke drawn around their perimeters
var w:Number = target_mc._width;
var h:Number = target_mc._height;
See also
Trang 3The following example loads an image into a draggable, dynamically created movie clip called
image_mc The number of bytes loaded and the total number of bytes for the loaded image display in a dynamically created text field called filesize_txt
See also
MovieClipLoader.onLoadProgress
Trang 4target The target path of a movie clip, or an integer specifying the level in Flash Player into which the movie will be loaded The target movie clip is replaced by the loaded SWF file
Using this method instead of loadMovie() or MovieClip.loadMovie() has a number of advantages The following handlers are implemented by the use on a listener object, which is registered with the MovieClipLoader class using
MovieClipLoader.addListener(listenerObject)
• The MovieClipLoader.onLoadStart handler is invoked when loading begins
• The MovieClipLoader.onLoadError handler is invoked if the clip cannot be loaded
• The MovieClipLoader.onLoadProgress handler is invoked as the loading process progresses
• The MovieClipLoader.onLoadInit handler is invoked after the actions in the first frame of the clip have executed, so you can begin manipulating the loaded clip
• The MovieClipLoader.onLoadComplete handler is invoked when a file has completed downloading
A SWF file or image loaded into a movie clip inherits the position, rotation, and scale properties
of the movie clip You can use the target path of the movie clip to target the loaded movie.You can use the method to load one or more files into a single movie clip or level;
Trang 5MovieClipLoader.loadClip() 605
MovieClipLoader.getProgress() and MovieClipLoaderListener.onLoadProgress do not report the actual bytesLoaded and bytesTotal values in the Authoring player when the files are local When you use the Bandwidth Profiler feature in the authoring environment,
MovieClipLoader.getProgress() and MovieClipLoaderListener.onLoadProgress report the download at the actual download rate, not at the reduced bandwidth rate that the Bandwidth Profiler provides
Example
The following example illustrates the use of many of the MovieClipLoader class methods and listeners:
// first set of listeners
var my_mcl:MovieClipLoader = new MovieClipLoader();
var myListener:Object = new Object();
myListener.onLoadStart = function(target_mc:MovieClip) {
trace("*********First my_mcl instance*********");
trace("Your load has begun on movie clip = "+target_mc);
var loadProgress:Object = my_mcl.getProgress(target_mc);
trace(loadProgress.bytesLoaded+" = bytes loaded at start");
trace(loadProgress.bytesTotal+" = bytes total at start");
};
myListener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number) {
trace("*********First my_mcl instance Progress*********");
trace("onLoadProgress() called back on movie clip "+target_mc);
trace(loadedBytes+" = bytes loaded at progress callback");
trace(totalBytes+" = bytes total at progress callback");
};
myListener.onLoadComplete = function(target_mc:MovieClip) {
trace("*********First my_mcl instance*********");
trace("Your load is done on movie clip = "+target_mc);
var loadProgress:Object = my_mcl.getProgress(target_mc);
trace(loadProgress.bytesLoaded+" = bytes loaded at end");
trace(loadProgress.bytesTotal+" = bytes total at end");
};
myListener.onLoadInit = function(target_mc:MovieClip) {
trace("*********First my_mcl instance*********");
trace("Movie clip = "+target_mc+" is now initialized");
// you can now do any setup required, for example:
trace("ERROR CODE = "+errorCode);
trace("Your load failed on movie clip = "+target_mc+"\n");
};
my_mcl.addListener(myListener);
// Now load the files into their targets.
// loads into movie clips
this.createEmptyMovieClip("clip1_mc", this.getNextHighestDepth());
clip1_mc._x = 400;
this.createEmptyMovieClip("clip2_mc", this.getNextHighestDepth());
Trang 6my_mcl.loadClip("http://www.macromedia.com/software/drk/images/box_drk5.jpg", clip1_mc);
// Second set of listeners
var another_mcl:MovieClipLoader = new MovieClipLoader();
var myListener2:Object = new Object();
myListener2.onLoadStart = function(target_mc:MovieClip) {
trace("*********Second my_mcl instance*********");
trace("Your load has begun on movie = "+target_mc);
var loadProgress:Object = my_mcl.getProgress(target_mc);
trace(loadProgress.bytesLoaded+" = bytes loaded at start");
trace(loadProgress.bytesTotal+" = bytes total at start");
};
myListener2.onLoadComplete = function(target_mc:MovieClip) {
trace("*********Second my_mcl instance*********");
trace("Your load is done on movie clip = "+target_mc);
var loadProgress:Object = my_mcl.getProgress(target_mc);
trace(loadProgress.bytesLoaded+" = bytes loaded at end");
trace(loadProgress.bytesTotal+" = bytes total at end");
};
myListener2.onLoadError = function(target_mc:MovieClip, errorCode:String) { trace("*********Second my_mcl instance*********");
trace("ERROR CODE = "+errorCode);
trace("Your load failed on movie clip = "+target_mc+"\n");
// Issue the following statements after the download is complete,
// and after my_mcl.onLoadInit has been called.
// my_mcl.removeListener(myListener);
// my_mcl.removeListener(myListener2);
See also
MovieClipLoader.unloadClip()
Trang 7listenerObject A listener object that was added using MovieClipLoader.addListener().
target_mc A movie clip loaded by a MovieClipLoader.loadClip() method This parameter is optional
Returns
Nothing
Description
Listener; invoked when a file loaded with MovieClipLoader.loadClip() has completely
downloaded The value for target_mc identifies the movie clip for which this call is being made This is useful if multiple files are being loaded with the same set of listeners
This parameter is passed by Flash to your code, but you do not have to implement all of the parameters in the listener function
When you use the onLoadComplete and onLoadInit events with the MovieClipLoader class, it’s important to understand how this differs from the way they work with your SWF file The
onLoadComplete event is called after the SWF or JPEG file has loaded, but before the application has been initialized At this point it is impossible to access the loaded movie clip’s methods and properties, and because of this you cannot call a function, move to a specific frame, and so on In most situations, it’s better to use the onLoadInit event instead, which is called after the content has loaded and is fully initialized
Example
The following example loads an image into a movie clip instance called image_mc The
onLoadInit and onLoadComplete events are used to determine how long it takes to load the image The information displays in a dynamically created text field called timer_txt
Trang 8target_mc.createTextField("timer_txt", target_mc.getNextHighestDepth(), 0, target_mc._height, target_mc._width, 22);
target_mc.timer_txt.text = "loaded in "+timerMS+" ms.";
};
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
image_mcl.loadClip("http://www.macromedia.com/images/shared/product_boxes/ 112x112/box_studio_112x112.jpg", image_mc);
See also
MovieClipLoader.addListener(), MovieClipLoader.onLoadStart,
MovieClipLoader.onLoadError
Trang 9listenerObject.onLoadError = function(target_mc:Object, errorCode:String) {
// your statements here
}
Parameters
listenerObject A listener object that was added using MovieClipLoader.addListener()
target_mc A movie clip loaded by a MovieClipLoader.loadClip() method
errorCode A string that explains the reason for the failure
Example
The following example displays information in the Output panel when an image fails to load This occurs when you test the following ActionScript, because the image does not exist in the specified location
this.createEmptyMovieClip("image_mc", this.getNextHighestDepth());
var mclListener:Object = new Object();
mclListener.onLoadError = function(target_mc:MovieClip, errorCode:String) { trace("ERROR!");
Trang 11listenerObject A listener object that was added using MovieClipLoader.addListener().
target_mc A movie clip loaded by a MovieClipLoader.loadClip() method This parameter is optional
The value for target_mc identifies the movie clip this call is being made for This is useful if you are loading multiple files with the same set of listeners This optional parameter is passed to your ActionScript
Example
The following example loads an image into a movie clip instance called image_mc The
onLoadInit and onLoadComplete events are used to determine how long it takes to load the image This information displays in a text field called timer_txt
target_mc.timer_txt.text = "loaded in "+timerMS+" ms.";
};
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
image_mcl.loadClip("http://www.macromedia.com/images/shared/product_boxes/ 112x112/box_studio_112x112.jpg", image_mc);
Trang 12See also
MovieClipLoader.onLoadStart
Trang 13listenerObject A listener object that was added using MovieClipLoader.addListener().
target_mc A movie clip loaded by a MovieClipLoader.loadClip() method This parameter is optional
loadedBytes The number of bytes that had been loaded when the listener was invoked
totalBytes The total number of bytes in the file being loaded
loadedBytes and totalBytes parameters
The value for target_mc identifies the movie clip this call is being made for This is useful if you are loading multiple files with the same set of listeners This optional parameter is passed to your ActionScript
Example
The following example creates a progress bar using the Drawing API The progress bar displays the loading progress of an image using the onLoadProgress listener When the image finishes loading, the progress bar is removed from the Stage You must replace the URL parameter of the
image_mcl.loadClip() command so that the parameter refers to a valid JPEG file using HTTP
If you attempt to use this example to load a local file that resides on your hard disk, this example will not work properly because, in test movie mode, Flash Player loads local files in their entirety Add the following ActionScript to your FLA or AS file:
Trang 15listenerObject A listener object that was added using MovieClipLoader.addListener().
target_mc A movie clip loaded by a MovieClipLoader.loadClip() method This parameter is optional
Returns
Nothing
Description
Listener; invoked when a call to MovieClipLoader.loadClip() has successfully begun to download
a file The value for target_mc identifies the movie clip this call is being made for This is useful
if you are loading multiple files with the same set of listeners This optional parameter is passed to your ActionScript
Example
The following example loads an image into a movie clip instance called image_mc The
onLoadInit and onLoadComplete events are used to determine how long it takes to load the image This information displays in a text field called timer_txt
target_mc.timer_txt.text = "loaded in "+timerMS+" ms.";
};
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
image_mcl.loadClip("http://www.macromedia.com/images/shared/product_boxes/ 112x112/box_studio_112x112.jpg", image_mc);
Trang 16See also
MovieClipLoader.onLoadError, MovieClipLoader.onLoadInit, MovieClipLoader.onLoadComplete
Trang 17Method; removes the listener that was used to receive notification when a MovieClipLoader
event handler was invoked No further loading messages will be received
Trang 18start_button.enabled = true;
stop_button.enabled = false;
//
image_mcl.removeListener(mclListener); };
stop_button.enabled = false;
Trang 19trace("\t name: "+target_mc._name);
trace("\t url: "+target_mc._url);
Trang 20Variable; a predefined variable with the IEEE-754 value for NaN (not a number) To determine if
a number is NaN, use isNaN()
Trang 21Constant; specifies the IEEE-754 value representing negative infinity The value of this constant
is the same as Number.NEGATIVE_INFINITY
CHAPTER 2
ActionScript Language Reference
Trang 22NetConnection class
Availability
Flash Player 7
Note: This class is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Description
The NetConnection class provides the means to play back streaming FLV files from a local drive
or HTTP address For more information on video playback, see “Playing back external FLV files
dynamically” in Using ActionScript in Flash
Method summary for the NetConnection class
Constructor for the NetConnection class
Availability
Flash Player 7
Note: This class is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see your Flash Communication Server documentation.
Constructor; creates a NetConnection object that you can use in conjunction with a NetStream
object to play back local streaming video (FLV) files After creating the NetConnection object,
use NetConnection.connect() to make the actual connection
Playing external FLV files provides several advantages over embedding video in a Flash document,
such as better performance and memory management, and independent video and Flash frame
NetConnection.connect() Opens a local connection through which you can play back video
(FLV) files from an HTTP address or from the local file system.
CHAPTER 2
ActionScript Language Reference
Trang 23NetConnection class 623 See also
NetStream class, Video.attachVideo()
Trang 24Availability
Flash Player 7
Note: This method is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Trang 25NetStream class 625
NetStream class
Availability
Flash Player 7
Note: This class is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Description
The NetStream class provides methods and properties for playing Flash Video (FLV) files from
the local file system or an HTTP address You use a NetStream object to stream video through a
NetConnection object Playing external FLV files provides several advantages over embedding
video in a Flash document, such as better performance and memory management, and
independent video and Flash frame rates This class provides a number of methods and properties
you can use to track the progress of the file as it loads and plays, and to give the user control over
playback (stopping, pausing, and so on)
For more information on video playback, see “Playing back external FLV files dynamically” in
Using ActionScript in Flash.
Method summary for the NetStream class
The following methods and properties of the NetConnection and NetStream classes are used to
control FLV playback
Property summary for the NetStream class
NetStream.close() Closes the stream but does not clear the video object.
NetStream.pause() Pauses or resumes playback of a stream.
NetStream.play() Begins playback of an external video (FLV) file.
NetStream.seek() Seeks a specific position in the FLV file.
NetStream.setBufferTime() Specifies how long to buffer data before starting to display the stream.
NetStream.bufferLength The number of seconds of data currently in the buffer.
NetStream.bufferTime Read-only; the number of seconds assigned to the buffer by
NetStream.currentFps The number of frames per second being displayed.
NetStream.time Read-only; the position of the playhead, in seconds.
CHAPTER 2
ActionScript Language Reference
Trang 26Event handler summary for the NetStream class
Constructor for the NetStream class
Availability
Flash Player 7
Note: This class is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
var connection_nc:NetConnection = new NetConnection();
NetConnection class, NetStream class, Video.attachVideo()
NetStream.onStatus Invoked every time a status change or error is posted for the
NetStream object.
Trang 27NetStream.bufferLength 627
NetStream.bufferLength
Availability
Flash Player 7
Note: This property is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Example
The following example dynamically creates a text field that displays information about the number of seconds that are currently in the buffer The text field also displays the buffer length that the video is set to, and percentage of buffer that is filled
this.createTextField("buffer_txt", this.getNextHighestDepth(), 10, 10, 300, 22);
var output_str:String = "<textformat tabStops='[100,200]'>";
output_str += "Length: "+my_ns.bufferLength+"\t"+"Time:
Trang 28Availability
Flash Player 7
Note: This property is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Example
The following example dynamically creates a text field that displays information about the number of seconds that are currently in the buffer The text field also displays the buffer length that the video is set to, and percentage of buffer that is filled
this.createTextField("buffer_txt", this.getNextHighestDepth(), 10, 10, 300, 22);
var output_str:String = "<textformat tabStops='[100,200]'>";
output_str += "Length: "+my_ns.bufferLength+"\t"+"Time:
Trang 29The following example creates a progress bar using the Drawing API and the bytesLoaded and
bytesTotal properties that displays the loading progress of video1.flv into the video object instance called my_video A text field called loaded_txt is dynamically created to display information about the loading progress as well
var connection_nc:NetConnection = new NetConnection();
this.createEmptyMovieClip("progressBar_mc", this.getNextHighestDepth()); progressBar_mc.createEmptyMovieClip("bar_mc",
Trang 30var loaded_interval:Number = setInterval(checkBytesLoaded, 500, stream_ns); function checkBytesLoaded(my_ns:NetStream) {
var pctLoaded:Number = Math.round(my_ns.bytesLoaded/my_ns.bytesTotal*100); loaded_txt.text = Math.round(my_ns.bytesLoaded/1000)+" of
"+Math.round(my_ns.bytesTotal/1000)+" KB loaded ("+pctLoaded+"%)";
Trang 31The following example creates a progress bar using the Drawing API and the bytesLoaded and
bytesTotal properties that displays the loading progress of video1.flv into the video object instance called my_video A text field called loaded_txt is dynamically created to display information about the loading progress as well
var connection_nc:NetConnection = new NetConnection();
this.createEmptyMovieClip("progressBar_mc", this.getNextHighestDepth()); progressBar_mc.createEmptyMovieClip("bar_mc",
Trang 33NetStream.close() 633
NetStream.close()
Availability
Flash Player 7
Note: This method is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Trang 34Availability
Flash Player 7
Note: This property is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
var fps_interval:Number = setInterval(displayFPS, 500, stream_ns);
function displayFPS(my_ns:NetStream) {
fps_txt.text = "currentFps (frames per second):
"+Math.floor(my_ns.currentFps);
}
Trang 35NetStream.onStatus 635
NetStream.onStatus
Availability
Flash Player 7
Note: This handler is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Usage
my_ns.onStatus = function(infoObject:Object) : Void{
// Your code here
}
Parameters
infoObject A parameter defined according to the status or error message For more
information about this parameter, see “Description,” below
Returns
Nothing
Description
Event handler; invoked every time a status change or error is posted for the NetStream object
If you want to respond to this event handler, you must create a function to process the
information object
The information object has a code property containing a string that describes the result of the
onStatus handler, and a level property containing a string that is either status or error
In addition to this onStatus handler, Flash also provides a “super” function called
System.onStatus If onStatus is invoked for a particular object and there is no function assigned
to respond to it, Flash processes a function assigned to System.onStatus if it exists
The following events notify you when certain NetStream activities occur
If you consistently see errors regarding buffer, you should try changing the buffer using the NetStream.setBufferTime() method
NetStream.Buffer.Empty status Data is not being received quickly enough to fill
the buffer Data flow will be interrupted until the buffer refills, at which time a
NetStream.Buffer.Full message will be sent and the stream will begin playing again.
NetStream.Buffer.Full status The buffer is full and the stream will begin
playing
NetStream.Play.Start status Playback has started.
NetStream.Play.Stop status Playback has stopped.
NetStream.Play.StreamNotFound error The FLV passed to the play() method can't be
found.
Trang 36The following example displays data about the stream in the Output panel:
var connection_nc:NetConnection = new NetConnection(); connection_nc.connect(null);
var stream_ns:NetStream = new NetStream(connection_nc); my_video.attachVideo(stream_ns);
Trang 37NetStream.pause() 637
NetStream.pause()
Availability
Flash Player 7
Note: This method is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
Usage
my_ns.pause( [ pauseResume:Boolean ] ) : Void
Parameters
pauseResume A Boolean value specifying whether to pause play (true) or resume play (false)
If you omit this parameter, NetStream.pause() acts as a toggle: the first time it is called on a specified stream, it pauses play, and the next time it is called, it resumes play This parameter is optional
Returns
Nothing
Description
Method; pauses or resumes playback of a stream
The first time you call this method (without sending a parameter), it pauses play; the next time, it resumes play You might want to attach this method to a button that the user presses to pause or resume playback
Example
The following examples illustrate some uses of this method:
my_ns.pause(); // pauses play first time issued
my_ns.pause(); // resumes play
my_ns.pause(false); // no effect, play continues
my_ns.pause(); // pauses play
See also
NetStream.close(), NetStream.play()
Trang 38Availability
Flash Player 7
Note: This method is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
If you want to control the audio associated with an FLV file, you can use
MovieClip.attachAudio() to route the audio to a movie clip; you can then create a Sound object to control some aspects of the audio For more information, see MovieClip.attachAudio()
If the FLV file can’t be found, the NetStream.onStatus event handler is invoked If you want to stop a stream that is currently playing, use NetStream.close()
You can play local FLV files that are stored in the same directory as the SWF file or in a
subdirectory; you can’t navigate to a higher-level directory For example, if the SWF file is located
in a directory named /training, and you want to play a video stored in the /training/videos directory, you would use the following syntax:
my_ns.play("videos/videoName.flv");
To play a video stored in the /training directory, you would use the following syntax:
my_ns.play("videoName.flv");
Example
The following example illustrates some ways to use the NetStream.play() command:
// Play a file that is on the user’s computer
// The joe_user directory is a subdirectory of the directory
Trang 39NetStream.play() 639
See also
MovieClip.attachAudio(), NetStream.close(), NetStream.pause(),
Video.attachVideo()
Trang 40Availability
Flash Player 7
Note: This method is also supported in Flash Player 6 when used with Flash Communication Server
For more information, see the Flash Communication Server documentation.
• To return to the beginning of the stream, pass 0 for numberOfSeconds
• To seek forward from the beginning of the stream, pass the number of seconds you want to advance For example, to position the playhead at 15 seconds from the beginning, use
my_ns.seek(15)
• To seek relative to the current position, pass my_ns.time + n or my_ns.time - n to seek n
seconds forward or backward, respectively, from the current position For example, to rewind 20 seconds from the current position, use my_ns.seek(my_ns.time - 20).
The precise location to which a video seeks will differ depending on the frames per second setting
at which it was exported Therefore, if the same video is exported at 6 fps and 30 fps, it will seek
to two different locations if you use, for example, my_ns.seek(15) for both video objects
The following example illustrates some ways to use the NetStream.seek() command:
// Return to the beginning of the stream