我写了一个小 swf Container,它应该加载一个视频文件并播放它。
我关注了 official Adobe documentation并且视频播放流畅。
之后我添加了一些 ExternalInterface API Callbacks 用于暂停、播放和恢复视频,它们都工作正常。这里只有一个非常烦人的问题:我无法获得有关视频当前状态的信息。通过 javascript 函数的调用和 swf 文件中的内部调用都不能提供一些正确的信息。我对此进行了一些深入研究,但没有找到有关此问题的信息。
这是 swf 文件的源代码和用于嵌入它的示例 html:
HTML
<object id="ie-video" width="1280" height="720" type="application/x-shockwave-flash" data="flash_container.swf">
<param name="movie" value="flash_container.swf" />
<param name="wmode" value="opaque" />
<param name="flashvars" value="vid_name=video&objWidth=1280&objHeight=720" />
<param name="AllowScriptAccess" value="always">
</object>
SWF
import flash.external.*;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.net.NetStream;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters.vid_name;
var objWidth:int = LoaderInfo(this.root.loaderInfo).parameters.objWidth;
var objHeight:int = LoaderInfo(this.root.loaderInfo).parameters.objHeight;
var videoPath:String = paramObj.toString() + ".flv";
var nc:NetConnection = new NetConnection();
nc.connect(null);
var vid:Video = new Video(objWidth, objHeight);
addChild(vid);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
var isVideoPlaying:Boolean;
var outputText:TextField = new TextField();
outputText.x = 10;
outputText.y = 10;
outputText.background = true;
outputText.autoSize = TextFieldAutoSize.LEFT;
addChild(outputText);
function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code) {
case 'NetStream.Play.Start':
//ExternalInterface.call("fadeInPack()");
outputText.text = event.info.code.toString();
break;
case 'NetStream.Play.Stop':
outputText.text = event.info.code.toString();
break;
case "NetConnection.Connect.Success":
outputText.text = event.info.code.toString();
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video");
break;
}
}
function asyncErrorHandler(event:AsyncErrorEvent):void
{
//ignore error
}
function videoPause() {
ns.pause();
isVideoPlaying = false;
}
function videoResume() {
ns.resume();
isVideoPlaying = true;
}
function getCurrentTime() {
outputText.text = ns.info.toString();
return ns.info;
}
function videoTogglePause() {
ns.togglePause();
isVideoPlaying = !isVideoPlaying;
}
function videoIsPlaying() {
return isVideoPlaying;
}
function videoPlay() {
isVideoPlaying = true;
vid.attachNetStream(ns);
ns.play(videoPath);
outputText.text = ns.info.toString();
}
ExternalInterface.addCallback("videoPause",videoPause);
ExternalInterface.addCallback("videoResume", videoResume);
ExternalInterface.addCallback("getCurrentTime", getCurrentTime);
ExternalInterface.addCallback("togglePause", videoTogglePause);
ExternalInterface.addCallback("videoIsPlaying", videoIsPlaying);
ExternalInterface.addCallback("videoPlay", videoPlay);
用于 ExternalApi 调用的 Javascript
element = document.getElementById('ie-video');
element.videoPlay();
element.getCurrentTime();
我尝试了不同的视频格式(flv 和 mp4),通过本地文件系统或 url 访问,但似乎没有任何效果。甚至信息对象的内部打印也没有提供任何信息。任何帮助表示赞赏。
最佳答案
您可能正在寻找 ns.time 属性
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html#time
ns.info 是一个包含不同属性的对象,您没有引用这些属性
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStreamInfo.html
关于flash - AS3 NetstreamInfo 类返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24937502/