ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Selecting and Playing Sound and Video | 347

The process of actually playing a video or sound file has been farmed out to classes


that take care of all the details. So the operation that plays the video or audio is


tucked away in another class, and all you need to do in the concrete classes is to


instantiate the respective classes and add the instances to a display list.


At this juncture, you need to be aware of the connection between the two opera-


tions. The order of the algorithm’s operations is still the same; however, both opera-


tions are linked by a common private string variable used to store the name of the


media. Notice that both concrete classes use theselectMedia( )andplayNow( )func-


tions, even though they’re used in different ways.


The Detail Classes


While the Template Method design pattern allows for details to be added to the


operations in the template method itself through the concrete subclasses, both the


video and audio operations are a bit more detailed. So rather than shoehorning them


into concrete classes, each has its own class. Of course, by doing so, you set up


classes that you can very likely use in other applications.


The class built for the video is fairly elaborate, and the one for the sound is quite sim-


ple. Open two new ActionScript files and save the code in Example 9-11 and


Example 9-12 using the caption names as the filenames. Be sure to save them in the


same directory as the others in the application.


Example 9-11. PlayVideo.as


package
{
import flash.display.Sprite;
import flash.media.Video;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.text.TextField;


public class PlayVideo extends Sprite
{
private var vidNow:Video;
private var durText:TextField;


public function PlayVideo(vid:String)
{
var nc:NetConnection=new NetConnection( );
nc.connect(null);
var ns:NetStream=new NetStream(nc);
vidNow=new Video( );
vidNow.attachNetStream(ns);
vid+=".flv";
ns.play(vid);
vidNow.x=((550/2)-(vidNow.width/2));
vidNow.y=50;

Free download pdf