如何按键盘才能播放一次视频

时间:2013-11-22 06:47:46

标签: actionscript-3 video flash-cs6

我再次来到这里寻求帮助:{我有一个问题,我试图谷歌但却无法找到答案。好吧,..可能有答案,但我不能让它有效吗?我正在学习AS3,所以让我说我还是新来的。

我正在做的是让一个keyboatd响应我拥有的vdo文件。按下播放是一个非常简单的想法。每个键都有他们的vdos可以播放,如果你按下另一个按钮,而第一个按钮仍然按下,它将播放其键的另一个vdo。我把它作为布尔函数使用keydown和keyup这样的函数:

import flash.events.Event;
 import flash.events.KeyboardEvent;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.media.Video;

var isLeft:Boolean = false;
var isRight:Boolean = false;
    var video;
var nc;
var ns;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onUP);
this.addEventListener(Event.ENTER_FRAME,playVid);

                nc = new NetConnection();
                nc.connect(null);
                ns = new NetStream(nc);
                ns.client = this;
                video = new Video(550,400);
                addChild(video);
                video.attachNetStream(ns);


function onDown(e:KeyboardEvent):void
{
      switch (e.keyCode)
      {
                case 37 :
                          //ns.play(TomAndJerry.flv);
                          isLeft=true;
                          break;
                case 39 :
                          //ns.play(westler.flv);
                          isRight = true;
                          break;
      }

}

function onUP(e:KeyboardEvent):void
{
      switch (e.keyCode)
      {
                case 37 :
                          isLeft = false;
                          break;
                case 39 :
                          isRight = false;
                          break;
      }
}

 function playVid(e:Event):void
{
      if (isLeft)
      {
                trace(kk);
                ns.play(westler.flv);
                isLeft = false;
      }
      else if (isRight)
      {
                trace(PP);
                ns.play(TomAndJerry.flv);
                //isRight = false;
      }

}

我尝试过不使用任何布尔值来制作一个keydown函数,或者尝试使用虚假的东西来播放一个vdo。它工作但是,我仍然有同样的问题,我找不到一个解决方案....

当您按住键盘按钮时,vdo将从头开始。

所有我想要的是即使按下键也要播放vdo。如果vdo结束然后再次作为循环播放,但如果键是up,则vdo将播放直到它结束。 如果有多个按钮按下,只需按下最新按下按钮的vdo。

T-T” 感谢。

聚苯乙烯。我尝试过removeEventListener,但它使每个按钮的功能都消失了。

1 个答案:

答案 0 :(得分:0)

每个帧调用你的playvid函数,所以我认为你的视频无法启动是正常的,我认为你可以尝试更改你的代码如下:

// add net status handler event to check the end of the video
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// remove this line    
//this.addEventListener(Event.ENTER_FRAME,playVid);

/** a key is pressed **/
function onDown(e:KeyboardEvent):void
{
    switch (e.keyCode)
    {
            case Keyboard.LEFT:
                      // start the video just if the video don't play
                      if(!isLeft) ns.play("TomAndJerry.flv");
                      // video left is playing
                      isLeft = true;
                      // video right isn't playing
                      isRight = false;
                      break;

            case Keyboard.RIGHT:
                      // start the video just if the video don't play
                      if(!isRight) ns.play("westler.flv");
                      // video rightis playing
                      isRight = true;
                      // video left isn't playing
                      isLeft = false;
                      break;
    }
}

/** a key is released **/
function onUP(e:KeyboardEvent):void
{
    switch (e.keyCode)
    {
            case Keyboard.LEFT:
                      isLeft = false;
                      break;
            case Keyboard.RIGHT:
                      isRight = false;
                      break;
    }
}

/** net status change, verify if we reach the end of the video **/
function netStatusHandler(e:NetStatusEvent):void
{
    // when netStatus code is NetStream.Play.Stop the video is complete
    if (e.info.code == "NetStream.Play.Stop") 
    {
        // right key is still pressed we loop the video
        if( isRight )      ns.play("westler.flv");
        // left key is still pressed we loop the video
        else if( isLeft )  ns.play("TomAndJerry.flv");
    }
}

我希望这会对你有所帮助:)。