Adding Video and Sound - Working with Media Classes
(Page 8 of 9 )
Using ActionScript 2.0's new media classes, you can effect far more precision than with any of the other tools available in Flash Pro. However, at the same time, many of the classes require Flash Communication Server for them to be of much use. Included in the media classes are the following:
Camera
Microphone
NetConnection
NetStream
Sound
Video
In looking at the classes within the media classes' ActionScript folder, you can see that only a portion can be applied without other programs. The first part of this chapter covered the Sound class, so that class will not be repeated here. The Camera and Microphone classes primarily are made up of a large number of read-only properties, and the methods and properties that allow writing are of little value without outward streaming or recording. Without Flash Communication Server, the only streaming that occurs is the progressive downloads of FLV files. Therefore, the remainder of this chapter shows how to employ the NetConnection, NetStream, and Video classes.
Which Media Component? - Some designers may wonder, why have the MediaController in the mix of media components? If you need a controller, just use the MediaPlayback, and if you don't, use the MediaDisplay. Seems like a no-brainer!
However, after some work with these components, you'll see that the separate display and controller turns out to be a very good idea. First, when you set the size of the display, you get exactly what you need for the design, and you maintain the 4:3 aspect ratio of the video. Once it has been sized, you can then add the controller separately. When you size the MediaPlayback component, though, the size includes both the display screen and the attached controller, so it's difficult to get an exact fit.
Second, unlike the MediaPlayback component, the MediaController is not connected to the display screen, so you can put it where you want. You can even change its orientation from horizontal to vertical if needed. So what may at first appear to be an unnecessary media component turns out to be an important design component.
Playing FLV Files with ActionScript 2.0 Prior to ActionScript 2.0, the only way you were able to play an FLV file was using Flash Communication Server. With Flash Pro using script, you can create very small files that can progressively download and display an FLV file. For example, a simple movie that steams an FLV file can be created in less than 1KB, whereas the minimum using components is about 67KB.
To get started, all you need on the Stage is a single embedded video object. The following steps show how to use the NetConnection and NetStream classes in conjunction with the Video class to display an FLV video:
Open a new Flash document and then open the Library panel. Add a total of three layers named, from top to bottom, Actions, Video, and Background.
In the Library panel, open the panel's pop-up menu and select New Video. An embedded video icon appears in the library.
Open the Color Mixer and in the large color pane, add the color #5C0B08 (dark maroon). Then open the Color Mixer's pop-up menu and select Add Swatch. Do the same thing with the colors #D4B707 (dark mustard) and #EADFC2 (light tan). Use the light tan as the background color for the movie.
Select the Background layer. In the middle of the Stage draw a rectangle with a dark mustard 3.75 stroke and a dark maroon fill. Use the Align tools to center the rectangle in the middle of the Stage. Lock the Background layer.
Select the Video layer and then drag an instance of the embedded video object to the center of the stage. In the Property Inspector, lock the object dimensions and change the W value to 300. (The H value should automatically change to 225.) Then, using the Align panel tools, center the Embedded Video object in the middle of the stage and give it the instance name flashVid_video.
Select the Actions layer and in the Actions panel, add the following script, substituting media5.flv for an FLV file in the same folder where you saved your FLA file:
//Make NetConnection
var makeConn_nc:NetConnection = new NetConnection(); makeConn_nc.connect(null);
//New NetStream
var netWork_ns:NetStream = new NetStream(makeConn_nc);
//Connect to Video instance
flashVid_video.attachVideo(netWork_ns);
netWork_ns.play("media5.flv");
Once you save the FLA file, test it. Its size is only 291 bytes (that's bytes, not kilobytes!). Because bandwidth is measured in bits instead of bytes, multiply the size by 8 to get the number of bits being shipped over the Internet—only about 2.3Kb—not even enough to get a 56K phone modem wheezing. The main bandwidth usage will come from progressively downloading the FLV file, but because this is done progressively, only a little at a time has to be sent and processed.
This chapter is from Macromedia Flash MX Professional 2004, by Bill Sanders (Sams, 2004, ISBN: 0672326051). Check it out at your favorite bookstore today.
Buy this book now. |
Next: NetStream Controls >>
More Flash Articles
More By Bill Sanders