mx.controls.streamingmedia.RTMPConnection類導讀

這些天在研究flashcom的mp3 庫.flashcom,我比較陌生.只在大四上的時候,花了一天半的時間搞過一個視頻錄製的.以前在工作室沒把音箱帶過去, 省得吵,結果一年多沒有聽到機器發聲.對音頻這方面知道的甚少, 快成盲了  . 在這裏非常感謝雲開的無私幫助.
看macromedia媒體組件的流媒體包,覺得還是收穫蠻大,以下分析的是基於RTMP協議的Netconnection的一個子類. 在mx.controls.streamingmedia包中. 

//****************************************************************************
//Copyright (C) 2003 Macromedia, Inc. All Rights Reserved.
//The following is Sample Code and is subject to all restrictions on
//such code as contained in the End User License Agreement accompanying
//this product.
//****************************************************************************
import mx.controls.streamingmedia.RTMPPlayer;
import mx.controls.streamingmedia.Tracer;//做調試的trace

/**
* A subclass of NetConnection that is tailored to work with the RTMPPlayer
* class.
* 一個專門爲RTMPPlayer定製的NetConnection類的子類
* @author Stephen Cheng
*/
class mx.controls.streamingmedia.RTMPConnection
extends NetConnection
{
        /** Flag to indicate whether a thread is already in connect(). Only one thread
        *  at a time should be in this function.
        * 標記是否已有一個線程在連接,同一時刻只能有一個線程連接
        */
        static var _connectFlag:Boolean;
        /** Array of queued RTMPPlayers, awaiting their turn to call connect(). */
        //RTMPPlayers隊列(實際上是RTMPConnection的數組),等待連接
        static var _connectorQueue:Array = new Array();
        
        private var _targetURI:String;//目標資源地點
        private var _streamName:String;//流的名稱
        private var _player:RTMPPlayer;//運行的RTMPPlayer實例
        
        //構造函數,參數爲一個RTMPPlayer的實例
        public function RTMPConnection(player:RTMPPlayer)
        {
                _player = player;
        }
        
        // 當元數據從flash communication server到來時
        // 播放器從服務器發來的信息設置總播放時間
        public function onMetaData(info)
        {
                _player.setTotalTime(info.duration);
        }
        
        /* Only one thread can be in this function at a time.
        * 同一時刻只有一個線程可以使用這個連接函數
        */
        public function connect(targetURI:String, streamName:String):Void
        {
                //如果已經存在一個線程正在連接
                //則把此連接放入到等待隊列中
                //並返回
                if (_connectFlag == true)
                {
                        pushConnection(targetURI, streamName);
                        return;
                }
                //如果不存在則把先把連接標記設爲true
                //然後再連接目標
                _connectFlag = true;
                super.connect(targetURI, streamName);
                
                //當前連接執行完後從隊列
                //彈出最近壓入的RTMPConnection實例,並執行連接
                popConnection();
        }
        
        private function pushConnection(targetURI:String, streamName:String):Void
        {
                //壓入隊列
                _targetURI = targetURI;
                _streamName = streamName;
                
                //queue the connection attempt and retry later
                _connectorQueue.push(this);
        }
        
        //彈出最近壓入的RTMPConnection實例,並執行連接
        private function popConnection():Void
        {
                _connectFlag = false;
                if ( _connectorQueue.length != 0)
                {
                        var poppedConnection:RTMPConnection = RTMPConnection(_connectorQueue.pop());
                        poppedConnection.connect(poppedConnection._targetURI, poppedConnection._streamName);
                }
        }
}
當中的連接隊列, 與其就是個隊列不如說是個堆棧.這樣使得連接變成先進後出了,這可不太好. 假如pop出來的RTMPConntion連接時間過長,此時又有新的連接.
那在棧底的豈不是永遠天日.而且連接也不分個等級,有可能因爲都是爲Player服務的,所以乾脆不分~~~~~ 好faint.
發佈了69 篇原創文章 · 獲贊 0 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章