我正在制作一个原型,我必须通过RTMP协议播放视频.我的代码如下:
private function init():void { streamID:String = "mp4:myVideo"; videoURL = "rtmp://fms.xstream.dk/*********.mp4"; vid = new video(); vid.width = 480; vid.height = 320; nc = new NetConnection(); nc.client = {onBWDone: function():void { }}; nc.addEventListener(NetStatusEvent.NET_STATUS,onConnectionStatus); nc.connect(videoURL); } private function onConnectionStatus(e:NetStatusEvent):void { if (e.info.code == "NetConnection.Connect.Success") { trace("Creating NetStream"); netStreamObj = new NetStream(nc); netStreamObj.client = new CustomClient(); netStreamObj.play(streamID); vid.attachNetStream(netStreamObj); addChild(vid); intervalID = setInterval(playback,1000); } } private function playback():void { trace((++counter) + " Buffer length: " + netStreamObj.bufferLength); } class CustomClient { public function onMetaData(info:Object):void { trace("Metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate); } public function onCuePoint(info:Object):void { trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type); }
}
但它没有播放,没有发生任何错误而且没有合作,如果有人有任何想法,请帮助我.
解决方法
这样做对我有用.我只是使用新闻频道的链接作为示例,因此请尝试使用您自己的流网址替换它. (ps:忽略像素化,这是一个低分辨率的示例链接).
另外..首先你有一个错字,你说vid = new video(); (意思是=新视频??).这可能是addChild(vid)行的问题吗?其次你需要asyncErrorHandler,onFCSubscribe和onBWDone等函数,我在使用RTMP时已经包含这些函数来阻止某些流丢失的错误(无论如何我在过去的经历中).此示例代码位于名为RTMP_test.as的文档类中(重命名为首选)…
package { import flash.display.*; import flash.events.*; import flash.net.*; import flash.media.*; import flash.system.*; import flash.utils.ByteArray; public class RTMP_test extends MovieClip { public var netStreamObj:NetStream; public var nc:NetConnection; public var vid:Video; public var streamID:String; public var videoURL:String; public var MetaListener:Object; public function RTMP_test () { init_RTMP(); } function init_RTMP():void { /* streamID = "mp4:myVideo"; videoURL = "rtmp://fms.xstream.dk/*********.mp4"; */ streamID = "QVCLive1@14308"; videoURL = "rtmp://cp79650.live.edgefcs.net/live/"; vid = new Video(); //typo! was "vid = new video();" nc = new NetConnection(); nc.addEventListener(NetStatusEvent.NET_STATUS,onConnectionStatus); nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler); nc.client = { onBWDone: function():void{} }; nc.connect(videoURL); } private function onConnectionStatus(e:NetStatusEvent):void { if (e.info.code == "NetConnection.Connect.Success") { trace("Creating NetStream"); netStreamObj = new NetStream(nc); MetaListener = new Object(); MetaListener.onMetaData = received_Meta; netStreamObj.client = MetaListener; netStreamObj.play(streamID); vid.attachNetStream(netStreamObj); addChild(vid); //intervalID = setInterval(playback,1000); } } private function playback():void { //trace((++counter) + " Buffer length: " + netStreamObj.bufferLength); } public function asyncErrorHandler(event:AsyncErrorEvent):void { trace("asyncErrorHandler.." + "\r"); } public function onFCSubscribe(info:Object):void { trace("onFCSubscribe - succesful"); } public function onBWDone(...rest):void { var p_bw:Number; if (rest.length > 0) { p_bw = rest[0]; } trace("bandwidth = " + p_bw + " Kbps."); } function received_Meta (data:Object):void { var _stageW:int = stage.stageWidth; var _stageH:int = stage.stageHeight; var _videoW:int; var _videoH:int; var _aspectH:int; var Aspect_num:Number; //should be an "int" but that gives blank picture with sound Aspect_num = data.width / data.height; //Aspect ratio calculated here.. _videoW = _stageW; _videoH = _videoW / Aspect_num; _aspectH = (_stageH - _videoH) / 2; vid.x = 0; vid.y = _aspectH; vid.width = _videoW; vid.height = _videoH; } } //end class } //end package
更新的代码:
>新的演示链接:现在QVC(英国购物)而不是今日俄罗斯(世界新闻).>添加行:nc.client = {onBWDone:function():void {}}; (因为Flash Player现在更严格了.没有这条线之前它工作正常).