I have a Flash SWF set up to play a netstream in response to keyboard commands. There are several very short video clips that are triggered by the number pad. Since there are so many (in this unfinished example there are only a few), two numbers must be pressed to play the video.
The first number triggers the videoArray function, and inside the videoArray function a new eventListener is created to play a video, while the old event listener is removed. Once the video finishes, I used NetStatusEvent to remove the netstream and add the videoArray eventListener again.
All of this works fine, however, There is a stopVideo function that is triggered by the S key. It has the same code as the NetStatusEvent to remove the netstream and re-ad the eventListener, but only the netstream is removed. The eventListener is not added.
Am I missing something that's stopping the eventListener from being added, or should I be doing this differently? Is it possible to skip to end of the video and trigger the NetStatusEvent to stop it?
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
// Create a NetConnection object
var nc:NetConnection = new NetConnection();
nc.connect(null);
// Create a NetStream object with NetConnection object as a parameter
var ns:NetStream = new NetStream(nc);
var vid:Video = new Video();
// Play control
stage.addEventListener(KeyboardEvent.KEY_UP,videoArray);
function preStop(event:KeyboardEvent):void {
if (event.keyCode == 97) {
trace("O Stop");
ns.close();
removeChild(vid);
vid.attachNetStream(null);
}
else if (event.keyCode == 98) {
trace("P Stop");
ns.close();
removeChild(vid);
vid.attachNetStream(null);
}
}
function videoArray(event:KeyboardEvent):void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN,preStop);
stage.removeEventListener(KeyboardEvent.KEY_UP,videoArray);
if (event.keyCode == 97) {
stage.addEventListener(KeyboardEvent.KEY_UP,play1);
trace("play1");
}
else if (event.keyCode == 98) {
stage.addEventListener(KeyboardEvent.KEY_UP,play2);
trace("play2");
}
// PLAY 1
function play1(event:KeyboardEvent):void {
if (event.keyCode == 97) {
stage.removeEventListener(KeyboardEvent.KEY_UP,play1);
stage.removeEventListener(KeyboardEvent.KEY_UP,play2);
// Play video
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("cfa.mov");
// Create a new Video object and attach NetStream object
vid.attachNetStream(ns);
addChild(vid);
}
else if (event.keyCode == 98) {
stage.removeEventListener(KeyboardEvent.KEY_UP,play1);
stage.removeEventListener(KeyboardEvent.KEY_UP,play2);
// Create a NetStream object with NetConnection object as a parameter
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("matilda.mov");
// Create a new Video object and attach NetStream object
vid.attachNetStream(ns);
addChild(vid);
}
stage.addEventListener(KeyboardEvent.KEY_UP,videoArray);
}
// PLAY 2
function play2(event:KeyboardEvent):void {
if (event.keyCode == 97) {
stage.removeEventListener(KeyboardEvent.KEY_UP,play1);
stage.removeEventListener(KeyboardEvent.KEY_UP,play2);
// Create a NetStream object with NetConnection object as a parameter
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("illusionists.mov");
// Create a new Video object and attach NetStream object
vid.attachNetStream(ns);
addChild(vid);
}
else if (event.keyCode == 98) {
stage.removeEventListener(KeyboardEvent.KEY_UP,play1);
stage.removeEventListener(KeyboardEvent.KEY_UP,play2);
// Create a NetStream object with NetConnection object as a parameter
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("janis.mp4");
// Create a new Video object and attach NetStream object
vid.attachNetStream(ns);
addChild(vid);
}
stage.addEventListener(KeyboardEvent.KEY_UP,videoArray);
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore error
}
}
// Stop at end of video
ns.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
function statusHandler(event:NetStatusEvent):void
{
trace(event.info.code)
if (event.info.code == 'NetStream.Buffer.Empty') {
ns.close();
removeChild(vid);
vid.attachNetStream(null);
stage.addEventListener(KeyboardEvent.KEY_DOWN,preStop);
stage.addEventListener(KeyboardEvent.KEY_UP,videoArray);
}
}
// Pause control
stage.addEventListener(KeyboardEvent.KEY_UP,togglePauseHandler);
function togglePauseHandler(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.SPACE) {
ns.togglePause();
}
}
// Stop control
stage.addEventListener(KeyboardEvent.KEY_UP,stopVideo);
function stopVideo(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.S) {
ns.close();
removeChild(vid);
vid.attachNetStream(null);
stage.addEventListener(KeyboardEvent.KEY_DOWN,preStop);
stage.addEventListener(KeyboardEvent.KEY_UP,videoArray);
}
}