我设置了Flash SWF以响应键盘命令播放网络流。有几个非常短的视频剪辑由数字键盘触发。由于有这么多(在这个未完成的例子中只有少数),必须按两个数字来播放视频。
第一个数字触发videoArray函数,在videoArray函数内部创建一个新的eventListener来播放视频,同时删除旧的事件监听器。视频播放完毕后,我使用NetStatusEvent删除了netstream并再次添加了videoArray eventListener。
所有这些都可以,但是,有一个由S键触发的stopVideo功能。它与NetStatusEvent具有相同的代码,用于删除netstream并重新添加eventListener,但只删除了netstream。不添加eventListener。
我是否遗漏了阻止添加eventListener的内容,或者我应该以不同方式执行此操作?是否可以跳到视频结尾并触发NetStatusEvent来停止它?
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);
}
}
答案 0 :(得分:1)
在videoArray函数中删除了事件侦听器,并且没有else语句。因此,只要按下另一个键,它就会禁用eventListeners,但不会触发play1,play2函数。
答案 1 :(得分:1)
以下是我将如何修改代码。更清晰,更容易理解单个键监听器的用途,而不是一直添加和删除不同的键监听器。请参阅代码注释
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;
}
}