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);
		}
	}
}

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章