10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

that take care of all the details. So the operation that plays the video or audio is<br />

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

instantiate the respective classes and add the instances to a display list.<br />

At this juncture, you need to be aware of the connection between the two operations.<br />

The order of the algorithm’s operations is still the same; however, both operations<br />

are linked by a <strong>com</strong>mon private string variable used to store the name of the<br />

media. Notice that both concrete classes use the selectMedia( ) and playNow( ) functions,<br />

even though they’re used in different ways.<br />

The Detail Classes<br />

While the Template Method design pattern allows for details to be added to the<br />

operations in the template method itself through the concrete subclasses, both the<br />

video and audio operations are a bit more detailed. So rather than shoehorning them<br />

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

classes that you can very likely use in other applications.<br />

The class built for the video is fairly elaborate, and the one for the sound is quite simple.<br />

Open two new <strong>ActionScript</strong> files and save the code in Example 9-11 and<br />

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

same directory as the others in the application.<br />

Example 9-11. PlayVideo.as<br />

package<br />

{<br />

import flash.display.Sprite;<br />

import flash.media.Video;<br />

import flash.events.NetStatusEvent;<br />

import flash.net.NetConnection;<br />

import flash.net.NetStream;<br />

import flash.text.TextField;<br />

public class PlayVideo extends Sprite<br />

{<br />

private var vidNow:Video;<br />

private var durText:TextField;<br />

public function PlayVideo(vid:String)<br />

{<br />

var nc:NetConnection=new NetConnection( );<br />

nc.connect(null);<br />

var ns:NetStream=new NetStream(nc);<br />

vidNow=new Video( );<br />

vidNow.attachNetStream(ns);<br />

vid+=".flv";<br />

ns.play(vid);<br />

vidNow.x=((550/2)-(vidNow.width/2));<br />

vidNow.y=50;<br />

Selecting and Playing Sound and Video | 347

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!