home *** CD-ROM | disk | FTP | other *** search
/ i·claim - visualizing argument / ICLAIM.ISO / pc / glossary.swf / scripts / __Packages / mx / controls / streamingmedia / RTMPPlayer.as < prev    next >
Encoding:
Text File  |  2005-02-24  |  11.8 KB  |  449 lines

  1. class mx.controls.streamingmedia.RTMPPlayer extends mx.controls.streamingmedia.AbstractPlayer
  2. {
  3.    var _mediaUrl;
  4.    var _mediaType;
  5.    var _videoHolder;
  6.    var _video;
  7.    var _totalTime;
  8.    var _listeners;
  9.    var _connectTimeOutId;
  10.    var _protocol;
  11.    var _port;
  12.    var _appUrl;
  13.    var _streamName;
  14.    var _isLoaded;
  15.    var _isLoading;
  16.    var _isPlayPending;
  17.    var _nc_rtmp;
  18.    var _nc_rtmpt;
  19.    var _playHeadPos;
  20.    var _isPausing;
  21.    var _sound;
  22.    var _ns;
  23.    var _videoHeight;
  24.    var _videoWidth;
  25.    var _player;
  26.    var popConnection;
  27.    var _conn_Id;
  28.    var _isSeeking;
  29.    var _momentaryPlayId;
  30.    var _statusCount;
  31.    var _checkCompleteId;
  32.    function RTMPPlayer(aMediaUrl, aMediaType, aVideoHolder, aTotalTime)
  33.    {
  34.       super();
  35.       if(aMediaUrl == null || aVideoHolder == null || aTotalTime == null)
  36.       {
  37.       }
  38.       this._mediaUrl = aMediaUrl;
  39.       this._mediaType = aMediaType;
  40.       this._videoHolder = aVideoHolder;
  41.       this._video = this._videoHolder._video;
  42.       this._totalTime = aTotalTime;
  43.       this._listeners = new Array();
  44.       this._connectTimeOutId = null;
  45.       this.init();
  46.    }
  47.    function init()
  48.    {
  49.       var _loc2_ = undefined;
  50.       _loc2_ = this._mediaUrl.indexOf(":");
  51.       if(_loc2_ == -1)
  52.       {
  53.       }
  54.       this._protocol = this._mediaUrl.substring(0,_loc2_).toLowerCase();
  55.       if(this._protocol != "rtmp" && this._protocol != "rtmps" && this._protocol != "rtmpt")
  56.       {
  57.       }
  58.       var _loc3_ = this._mediaUrl.substring(_loc2_ + 1,this._mediaUrl.length);
  59.       this._port = null;
  60.       var _loc5_ = _loc3_.indexOf(":");
  61.       if(_loc5_ != -1)
  62.       {
  63.          this._port = _loc3_.substring(_loc5_ + 1,_loc3_.length);
  64.          var _loc4_ = this._port.indexOf("/");
  65.          if(_loc4_ != -1 && _loc4_ < _loc5_)
  66.          {
  67.             this._port = this._port.substring(0,_loc4_);
  68.          }
  69.          else
  70.          {
  71.             this._port = null;
  72.          }
  73.       }
  74.       _loc2_ = _loc3_.lastIndexOf("/");
  75.       if(_loc2_ == -1)
  76.       {
  77.       }
  78.       this._appUrl = _loc3_.substring(0,_loc2_);
  79.       this._streamName = _loc3_.substring(_loc2_ + 1,_loc3_.length);
  80.       if(this._streamName.length == 0)
  81.       {
  82.       }
  83.       if(this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE)
  84.       {
  85.          _loc2_ = this._streamName.indexOf(".");
  86.          if(_loc2_ != -1)
  87.          {
  88.             var _loc6_ = undefined;
  89.             _loc6_ = this._streamName.substring(_loc2_).toLowerCase();
  90.             if(_loc6_ == ".flv")
  91.             {
  92.                this._streamName = this._streamName.substring(0,_loc2_);
  93.             }
  94.          }
  95.          this._streamName = "flv:" + this._streamName;
  96.       }
  97.       else if(this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.MP3_MEDIA_TYPE)
  98.       {
  99.          _loc2_ = this._streamName.indexOf(".");
  100.          if(_loc2_ != -1)
  101.          {
  102.             _loc6_ = this._streamName.substring(_loc2_).toLowerCase();
  103.             if(_loc6_ == ".mp3")
  104.             {
  105.                this._streamName = this._streamName.substring(0,_loc2_);
  106.             }
  107.          }
  108.          this._streamName = "mp3:" + this._streamName;
  109.       }
  110.       this.setPlaying(false);
  111.       this._isLoaded = false;
  112.       this._isLoading = false;
  113.       this._isPlayPending = false;
  114.       this._nc_rtmp = null;
  115.       this._nc_rtmpt = null;
  116.       this._playHeadPos = 0;
  117.       this.setSeeking(false);
  118.       this._isPausing = false;
  119.       this._sound = new Sound(this._videoHolder);
  120.       this.setVolume(mx.controls.streamingmedia.StreamingMediaConstants.DEFAULT_VOLUME);
  121.    }
  122.    function addListener(aListener)
  123.    {
  124.       this._listeners.push(aListener);
  125.    }
  126.    function removeAllListeners()
  127.    {
  128.       this._listeners.length = 0;
  129.    }
  130.    function broadcastEvent(status)
  131.    {
  132.       var _loc2_ = 0;
  133.       while(_loc2_ < this._listeners.length)
  134.       {
  135.          this._listeners[_loc2_].handlePlayer(this,status);
  136.          _loc2_ = _loc2_ + 1;
  137.       }
  138.    }
  139.    function bufferIsFull()
  140.    {
  141.       this.broadcastEvent("start");
  142.       if(!this.isPlaying())
  143.       {
  144.          this.pause();
  145.       }
  146.    }
  147.    function resizeVideo()
  148.    {
  149.       this.broadcastEvent("resizeVideo");
  150.       if(!this.isPlaying())
  151.       {
  152.          this.pause();
  153.       }
  154.    }
  155.    function toString()
  156.    {
  157.       return "RTMPPlayer: Playing " + this.getMediaUrl();
  158.    }
  159.    function close()
  160.    {
  161.       this._ns.onStatus = null;
  162.       this._ns.close();
  163.       this._ns = null;
  164.       if(this._nc_rtmp != null)
  165.       {
  166.          this._nc_rtmp.onStatus = null;
  167.          this._nc_rtmp.close();
  168.          this._nc_rtmp = null;
  169.       }
  170.       if(this._nc_rtmpt != null)
  171.       {
  172.          this._nc_rtmpt.onStatus = null;
  173.          this._nc_rtmpt.close();
  174.          this._nc_rtmpt = null;
  175.       }
  176.       this._video.clear();
  177.    }
  178.    function load()
  179.    {
  180.       this._isLoading = true;
  181.       this.actualConnect();
  182.       if(this._connectTimeOutId != null)
  183.       {
  184.          clearInterval(this._connectTimeOutId);
  185.          this._connectTimeOutId = null;
  186.       }
  187.       this._connectTimeOutId = setInterval(this,"onConnectTimeOut",60000);
  188.    }
  189.    function startStream(nc)
  190.    {
  191.       clearInterval(this._connectTimeOutId);
  192.       this._connectTimeOutId = null;
  193.       this._ns = new mx.controls.streamingmedia.PlayerNetStream(nc,this);
  194.       if(this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE)
  195.       {
  196.          this._video.attachVideo(this._ns);
  197.       }
  198.       this._video.attachVideo(this._ns);
  199.       this._videoHeight = this._video.height;
  200.       this._videoWidth = this._video.width;
  201.       this._ns.setBufferTime(5);
  202.       this._videoHolder.attachAudio(this._ns);
  203.       this._ns.play(this._streamName,0,-1);
  204.       this._isLoading = false;
  205.       this._isLoaded = true;
  206.       this._videoHolder._visible = false;
  207.       this.setPlaying(false);
  208.       if(this._isPlayPending)
  209.       {
  210.          this.play(null);
  211.       }
  212.       else
  213.       {
  214.          this._isPausing = false;
  215.          this.pause();
  216.       }
  217.    }
  218.    function onConnectTimeOut()
  219.    {
  220.       clearInterval(this._connectTimeOutId);
  221.       this._connectTimeOutId = null;
  222.       if(this._nc_rtmpt != null)
  223.       {
  224.          this._nc_rtmpt.onStatus = null;
  225.          this._nc_rtmpt.close();
  226.          this._nc_rtmpt = null;
  227.       }
  228.       if(this._nc_rtmp != null)
  229.       {
  230.          this._nc_rtmp.onStatus = null;
  231.          this._nc_rtmp.close();
  232.          this._nc_rtmp = null;
  233.       }
  234.       this._isLoading = false;
  235.       this._isLoaded = false;
  236.    }
  237.    function actualConnect()
  238.    {
  239.       if(this._protocol == "rtmp")
  240.       {
  241.          this._nc_rtmp = new mx.controls.streamingmedia.RTMPConnection(this);
  242.          this._nc_rtmp.onStatus = function(info)
  243.          {
  244.             if(info.code == "NetConnection.Connect.Success")
  245.             {
  246.                clearInterval(this._player._conn_Id);
  247.                this._nc_rtmpt.onStatus = null;
  248.                this._nc_rtmpt.close();
  249.                this._nc_rtmpt = null;
  250.                this._player.startStream(this);
  251.                this.popConnection();
  252.             }
  253.          };
  254.          this._nc_rtmp.connect("rtmp:" + this._appUrl,this._streamName);
  255.       }
  256.       if(this._protocol == "rtmpt" || this._protocol == "rtmp" && this._port == null)
  257.       {
  258.          this._nc_rtmpt = new mx.controls.streamingmedia.RTMPConnection(this);
  259.          this._nc_rtmpt.onStatus = function(info)
  260.          {
  261.             if(info.code == "NetConnection.Connect.Success")
  262.             {
  263.                this._nc_rtmp.onStatus = null;
  264.                this._nc_rtmp.close();
  265.                this._nc_rtmp = null;
  266.                this._player.startStream(this);
  267.                this.popConnection();
  268.             }
  269.          };
  270.          if(this._protocol == "rtmpt")
  271.          {
  272.             this._nc_rtmpt.connect("rtmpt:" + this._appUrl,this._streamName);
  273.          }
  274.          else
  275.          {
  276.             clearInterval(this._conn_Id);
  277.             this._conn_Id = setInterval(this,"connectRtmpt",3000);
  278.          }
  279.       }
  280.    }
  281.    function connectRtmpt()
  282.    {
  283.       clearInterval(this._conn_Id);
  284.       this._nc_rtmpt.connect("rtmpt:" + this._appUrl,this._streamName);
  285.    }
  286.    function play(startingPoint)
  287.    {
  288.       this.setPlaying(true);
  289.       if(startingPoint != null)
  290.       {
  291.          this._playHeadPos = startingPoint;
  292.       }
  293.       if(this._isLoading || !this._isLoaded)
  294.       {
  295.          this._isPlayPending = true;
  296.          if(!this._isLoaded)
  297.          {
  298.             this.load();
  299.          }
  300.          return undefined;
  301.       }
  302.       this._isPlayPending = false;
  303.       this._isPausing = false;
  304.       this._ns.pause(false);
  305.       this._ns.seek(this._playHeadPos);
  306.    }
  307.    function pause()
  308.    {
  309.       if(!this._isPausing)
  310.       {
  311.          this._ns.pause(true);
  312.          this._isPausing = true;
  313.          this._isPlayPending = false;
  314.          this._playHeadPos = this._ns.time;
  315.          this.setPlaying(false);
  316.       }
  317.    }
  318.    function stop()
  319.    {
  320.       this.pause();
  321.       this.setPlayheadTime(0);
  322.    }
  323.    function getPlayheadTime()
  324.    {
  325.       return this._ns.time;
  326.    }
  327.    function setPlayheadTime(position)
  328.    {
  329.       this._playHeadPos = position;
  330.       if(!this._isSeeking)
  331.       {
  332.          this._ns.seek(position);
  333.          this.setSeeking(true);
  334.       }
  335.       if(!mx.controls.streamingmedia.StreamingMediaConstants.SCRUBBING)
  336.       {
  337.          if(!this.isPlaying())
  338.          {
  339.             this._ns.pause(false);
  340.             clearInterval(this._momentaryPlayId);
  341.             this._momentaryPlayId = setInterval(this,"doneUpdateFrame",50);
  342.          }
  343.       }
  344.    }
  345.    function doneUpdateFrame()
  346.    {
  347.       clearInterval(this._momentaryPlayId);
  348.       this._momentaryPlayId = null;
  349.       this._ns.pause(true);
  350.    }
  351.    function playStopped()
  352.    {
  353.       this._statusCount = 2;
  354.       clearInterval(this._checkCompleteId);
  355.       this._checkCompleteId = setInterval(this,"checkComplete",50);
  356.    }
  357.    function checkComplete()
  358.    {
  359.       if(this._ns.bufferLength <= 0.001)
  360.       {
  361.          if(this._statusCount <= 0)
  362.          {
  363.             clearInterval(this._checkCompleteId);
  364.             this._checkCompleteId = null;
  365.             this.pause();
  366.             this.broadcastEvent("complete");
  367.          }
  368.          else
  369.          {
  370.             this._statusCount -= 1;
  371.          }
  372.       }
  373.    }
  374.    function getMediaUrl()
  375.    {
  376.       return this._mediaUrl;
  377.    }
  378.    function setMediaUrl(aUrl)
  379.    {
  380.       this._mediaUrl = aUrl;
  381.       this._isLoaded = false;
  382.       var _loc3_ = this.isPlaying();
  383.       var _loc2_ = this.getVolume();
  384.       this.close();
  385.       this.init();
  386.       this.setVolume(_loc2_);
  387.       if(_loc3_)
  388.       {
  389.          this.play(0);
  390.       }
  391.       else
  392.       {
  393.          this.load();
  394.       }
  395.    }
  396.    function getVolume()
  397.    {
  398.       return this._sound.getVolume();
  399.    }
  400.    function setVolume(aVol)
  401.    {
  402.       this._sound.setVolume(aVol);
  403.    }
  404.    function getMediaBytesLoaded()
  405.    {
  406.       return this._ns.bytesLoaded;
  407.    }
  408.    function getMediaBytesTotal()
  409.    {
  410.       return this._ns.bytesTotal;
  411.    }
  412.    function getTotalTime()
  413.    {
  414.       return this._totalTime;
  415.    }
  416.    function setTotalTime(aTime)
  417.    {
  418.       this._totalTime = aTime;
  419.    }
  420.    function mediaLoaded()
  421.    {
  422.    }
  423.    function logError(error)
  424.    {
  425.    }
  426.    function isSizeSet()
  427.    {
  428.       if(this._video.width > 0 && this._video.height > 0)
  429.       {
  430.          return true;
  431.       }
  432.       return false;
  433.    }
  434.    function isSizeChange()
  435.    {
  436.       if(this._video.width != this._videoWidth || this._video.height != this._videoHeight)
  437.       {
  438.          this._videoWidth = this._video.width;
  439.          this._videoHeight = this._video.height;
  440.          return true;
  441.       }
  442.       return false;
  443.    }
  444.    function setSeeking(isSeeking)
  445.    {
  446.       this._isSeeking = isSeeking;
  447.    }
  448. }
  449.