Video Player Concrete State Application | 367
Because the application at this stage only provides traces, you will need to use the
Flash Test (Control➝Test or Control➝Test Project) to see the output. Open a new
Flash document, and, in the Document class window, type inTestState. The follow-
ing should appear in the Output window:
Video Player is On
--Play State--
--Stop State--
Begin playing
A new state is set
You're already playing
Stop playing.
A new state is set
You're already stopped
Both theVideoWorksclass and thePlayStateandStopStateclasses include atrace
statement to indicate their instantiation, and appear as soon as you test the script.
Because the initial state is Stop, it changes states to the Play state when the first
startPlay( )method is invoked. Also, because you’re changing states, atracestate-
ment from theVideoWorksclass indicates a state change. However, when the second
startPlay( )method is invoked a second time, the same method in a different con-
text recognizes that it’s already in the Play state and simply indicates that you’re
already playing. When you press the Stop button, you move to the Stop state, set-
ting a new state, but on the second press, the same function in this new context rec-
ognizes that you’re already in the Stop state, and indicates that fact.
Video Player Concrete State Application
All that you’ve seen so far has been the output astrace( )statements to help under-
stand how a State design pattern and State machine works. To add something use-
ful, we need to include a reference to both aNetStreamobject and a string for
referencing an FLV file. However, we need a string reference only for playing the
video, because we can stop it simply by closing theNetStreaminstance. The follow-
ing four scripts set up the state machine to actually play and stop a video. All of the
trace statements have been left in place.
With the implementation of an application that actually plays a video, we’ll need to
import the necessary parts. Because theNetStreamclass is used in the interface and
the two states, each of those files will need to import the class. However, while the
VideoWorksclass uses both thePlayandStopclasses, it does not have to import the
NetStream( ) class. This is because it’s already imported in thePlay andStop classes.
The following five listings, Example 10-7 through Example 10-11, should be entered
into an ActionScript file and saved with the captions as the filenames. Save all the
files in the same folder.