0

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);
    }
}
  • I haven't looked too closely (you posted a lot of code that isn't super easy to read), but your issue is likely a result of inline functions being used as event listeners. Everytime the `videoArray` function is called, it creates a whole new play1/play set of methods that are not the same as the previous ones attached to your listener. – BadFeelingAboutThis Jul 26 '16 at 20:33
  • As I tried to do what you suggested, I noticed that the event listeners were being removed in the videoArray function, and there was also no else statement. So any time another key was pressed (in this case S), it was disabling the eventListeners, but not triggering the play1, play2 functions. thanks for taking a look. – Browntastic Jul 26 '16 at 21:08

2 Answers2

1

The event listeners are removed in the videoArray function, and there is no else statement. So any time another key is pressed, it disables the eventListeners, but doesn't trigger the play1, play2 functions.

  • This does indeed seem to be likely issue. This and perhaps that the listeners are only added again if you hit `S` or the netstream buffer empties. – BadFeelingAboutThis Jul 26 '16 at 21:22
1

Here is how I would rework your code. Much cleaner and easier to figure out what's going with a single key listener and to not be adding and removing different key listeners all the time. See the code comments

import flash.events.AsyncErrorEvent;
import flash.events.NetStatusEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
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);

//add your netstream listeners just once (the next line was in your play functions)
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 
ns.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);

var vid:Video = new Video();



var firstNum:int = 0; //to track the first number pressed in a set

//a list of all your videos, this example supports 81 items in this array/list.
var streams:Array = ["matilda.mov", "cfa.mov", "illusionists.mov", "janis.mp4"];

// A single Key up listener to handle everything
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

function asyncErrorHandler(event:AsyncErrorEvent):void { 
    //ignore error 
}

//write the close and open stream code just once
function closeStream(event:KeyboardEvent):void {
    ns.close();
    removeChild(vid);
    vid.attachNetStream(null);
}

function openStream(path:String):void {
    // Play video
    ns.play(path); 

    // Create a new Video object and attach NetStream object
    vid.attachNetStream(ns); 
    addChild(vid);  
}

function keyUp(event:KeyboardEvent):void {
    //if they key event is numpad key
    if (event.keyCode >= Keyboard.NUMPAD_1 && event.keyCode <= Keyboard.NUMPAD_9) {
        var index:int = event.keyCode - Keyboard.NUMPAD_0; //which number was pushed between 1 - 9


        if (firstNum < 1) {
            //if this is the first number press, just assign that number
            firstNum = index;
        }else {
            //if the second press, play the appropriate video

            //this is the math for finding the video number
            index = ((firstNum - 1) * 9) + index - 1; // -1 one at the end since arrays are 0 based

            //if the number is higher the amount of videos available, set the index to the last video in the list
            if (streams.length >= index) {
                index = streams.length - 1;
            }

            //play the video
            openStream(streams[index]);

            //reset the firstNum
            firstNum = 0;
        }

        return; //don't look at any other key presses below since there's no reason to
    } 

    switch(event.keyCode) {
        case Keyboard.SPACE:
            ns.togglePause();
            break;

        case Keyboard.S:
            closeStream();
            break;
    }
}

function statusHandler(event:NetStatusEvent):void { 
    trace(event.info.code)

    switch(event.info.code){
        case 'NetStream.Buffer.Empty':
        case 'NetStream.Play.Stop':
        case 'NetStream.Play.Complete':
            closeStream();
            break;
    }
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40