368 | Chapter 10: State Pattern
In the followingStopStateclass shown in Example 10-8, you will note that the
startPlay( )method has been implemented to actually play the selected FLV file.
That transition to the Play state is not to start playing the video, but rather to set the
state where the video is playing.
Note that in thePlayStateclass shown in Example 10-9, thestartPlay( )method
wisely does nothing other than issue a statement to remind the user that the video is
already playing. In the test mode, the message appears in the Output window, but
when the user’s working with it, no message is issued. The feedback issues from the
playing video.
Example 10-7. State.as
package
{
//State Interface #1
import flash.net.NetStream;
interface State
{
function startPlay(ns:NetStream,flv:String):void;
function stopPlay(ns:NetStream):void;
}
}
Example 10-8. StopState.as
package
{
//Stop State #2
import flash.net.NetStream;
class StopState implements State
{
private var videoWorks:VideoWorks;
public function StopState(videoWorks:VideoWorks)
{
trace("--Stop State--");
this.videoWorks=videoWorks;
}
public function startPlay(ns:NetStream,flv:String):void
{
ns.play(flv);
trace("Begin playing");
videoWorks.setState(videoWorks.getPlayState( ));
}
public function stopPlay(ns:NetStream):void
{
trace("You're already stopped");
}
}
}