netstream播放rtmp直播流卡顿
标准的flash播放器ActionScript3语句,播放flash publish的rtmp流,
NetConnection--->NetStream--->play--->attachNetStream
然而项目中这么做,一直处于卡顿状态。
后来添加了一句
nsPlayer.bufferTime = 0.1;
居然不卡顿了。
帮助文档说:
The default value is 0.1 (one-tenth of a second). To determine the number of seconds currently in the buffer, use the bufferLength property.
与实际上不符合,我测试nsPlayer.bufferTime = 0;果然是卡顿,和不设置一样。意味着默认是0而不是0.1。
所有的代码如下:
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.NetStatusEvent; import flash.external.ExternalInterface; import flash.media.Camera; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; import flash.system.Security; import flash.ui.ContextMenu; import flash.utils.setTimeout; import flash.text.TextField; public class user extends Sprite { private var nc:NetConnection; private var nsPlayer:NetStream; private var vidPlayer:Video; private var cam:Camera; private var flashvars:Object; private var rtmpUrl:String; private var screen_w:int=640; private var screen_h:int=360; public function user() { if (!this.stage) { this.addEventListener(Event.ADDED_TO_STAGE, this.system_on_add_to_stage); } else { this.system_on_add_to_stage(null); } } /** * system event callback, when this control added to stage. * the main function. */ private function system_on_add_to_stage(evt:Event):void { this.removeEventListener(Event.ADDED_TO_STAGE, this.system_on_add_to_stage); trace("[Jeffer] js not ready, try later."); this.stage.align = StageAlign.TOP_LEFT; this.stage.scaleMode = StageScaleMode.NO_SCALE; this.contextMenu = new ContextMenu(); this.contextMenu.hideBuiltInItems(); flashvars = this.root.loaderInfo.parameters; if (!flashvars.hasOwnProperty("id")) { // throw new Error("must specifies the id"); } this.rtmpUrl = flashvars.rtmpUrl; Security.allowDomain("*"); Security.allowInsecureDomain("*"); flash.utils.setTimeout(this.system_on_js_ready, 500); } /** * system callack event, when js ready, register callback for js. * the actual main function. */ private function system_on_js_ready():void { if (!flash.external.ExternalInterface.available) { trace("[Jeffer] js not ready, try later."); flash.utils.setTimeout(this.system_on_js_ready, 100); return; } var label:TextField = new TextField(); label.x = 20; label.y = 20; label.width = 400; label.text="拉流地址:"+(rtmpUrl==null?"":rtmpUrl); addChild(label); if(rtmpUrl != null){ var tcpUrl:String = rtmpUrl.substr(0, rtmpUrl.lastIndexOf("/")); nc = new NetConnection(); nc.client = {}; nc.client.onBWDone = function():void {}; nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); nc.connect(tcpUrl); //注意服务器的IP地址。。。不然显示不出来 trace("输出调试信息."); } } private function onNetStatus(event:NetStatusEvent):void{ trace(event.info.code); if(event.info.code == "NetConnection.Connect.Success"){ displayPlaybackVideo(); } } private function displayPlaybackVideo():void{ var streamName:String = this.rtmpUrl.substr(this.rtmpUrl.lastIndexOf("/")); streamName.replace("/",""); nsPlayer = new NetStream(nc); nsPlayer.bufferTime = 0; nsPlayer.play(streamName); vidPlayer = new Video(screen_w, screen_h); vidPlayer.x = 0; vidPlayer.y = 0; vidPlayer.attachNetStream(nsPlayer); addChild(vidPlayer); } } }