使用AS3循环播放MP4视频

时间:2018-08-10 04:20:42

标签: actionscript-3 video mp4

我正在尝试让一个简单的加载的MP4视频在后台循环播放。

组件名称为“ videoloop” 文件名为“ OTG_MainLoop_1080p.mp4”

这是代码。

videoloop.source="Content/OTG_MainLoop_1080p.mp4"
videoloop.autoRewind=true
videoloop.addEventListener(VideoEvent.AUTO_REWOUND, videoPlayAgain, 
false, 0, true)
function videoPlayAgain( event:VideoEvent):void
{
 event.target.play()
}

这是我在控制台中遇到的错误,如果有帮助的话。

TypeError:错误#1034:类型强制失败:无法将fl.video::VideoEvent@460cf159转换为flash.events.VideoEvent。     在flash.events::EventDispatcher/dispatchEventFunction()     在flash.events::EventDispatcher/dispatchEvent()     在fl.video::FLVPlayback/http://www.adobe.com/2007/flash/flvplayback/internal::handleVideoEvent()     在flash.events::EventDispatcher/dispatchEventFunction()     在flash.events::EventDispatcher/dispatchEvent()     在fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::setState()     在fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::setStateFromCachedState()     在fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::httpDoSeek()     在flash.utils :: Timer / _timerDispatch()     在flash.utils :: Timer / tick()

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

您的错误是因为 AS3 编译器将FLV组件自身的内部videoEvent #1与内置的 other Video Event #2混淆了用于基于VideoNetStream的对象。

这两种类型不兼容,因此您不能强制(强制)type #1使其像type #2那样工作。 ...至少在使用 AS3 语言时没有。

解决方案:

如果您收听 event is "complete" 信号,它会起作用:

videoloop.source="Content/OTG_MainLoop_1080p.mp4";
//videoloop.autoRewind=true; //is not needed.

videoloop.addEventListener("complete", videoPlayAgain, false, 0, true);

function videoPlayAgain (event:Event):void
{
    event.target.seek(0);
    event.target.play();
}

FLV组件有时会出现一些重播故障(例如:在某些循环后,视频冻结了,然后在几秒钟后正常播放)。我认为这取决于输出视频文件时的编码器设置。

您可能更喜欢使用NetStreamVideo API来处理视频播放。

//# setup Video object and related Net Stream/Connection objects
var vidloop:Video = new Video(); addChild(vidloop);
vidloop.width = stage.stageWidth; vidloop.height = stage.stageHeight;

var vidNC:NetConnection = new NetConnection(); vidNC.connect(null);
var vidNS:NetStream = new NetStream(vidNC); vidloop.attachNetStream(vidNS);

//# metadata handler to correctly display video
var metaListener :Object = new Object(); metaListener = { onMetaData: process_Metadata };
vidNS.client = metaListener;

//# play video file
vidNS.addEventListener(NetStatusEvent.NET_STATUS, videoStatusHandler); //checks for ending
vidNS.play("Content/OTG_MainLoop_1080p.mp4");


function videoStatusHandler (event:NetStatusEvent):void 
{
    if (event.info.code == "NetStream.Buffer.Empty") //buffer is empty when video ends...
    {
        //# ...so rewind and play again.
        vidNS.seek(0); vidNS.resume();
    }
}


function process_Metadata (in_Data :Object):void
{
    //check metadata like width, height, duration etc
    //trace("duration is : " + in_Data.duration );
}