home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Nave / sky-chopper.swf / scripts / __Packages / mx / controls / MediaDisplay.as < prev    next >
Encoding:
Text File  |  2005-09-29  |  17.8 KB  |  709 lines

  1. class mx.controls.MediaDisplay extends mx.core.UIComponent implements mx.controls.streamingmedia.ICuePointHolder
  2. {
  3.    var _deadPreview;
  4.    var _enabled;
  5.    var _playingBeforeDisabled;
  6.    var _started;
  7.    var _sendCompleteEvent;
  8.    var _fullyLoaded;
  9.    var _contentPath;
  10.    var _screenAccommodator;
  11.    var _cuePoints;
  12.    var initCuePointNames;
  13.    var initCuePointTimes;
  14.    var _playerImpl;
  15.    var _videoHolder;
  16.    var _totalTime;
  17.    var _mediaType;
  18.    var _soundHolder;
  19.    var _autoPlay;
  20.    var _pollId;
  21.    var _autoSize;
  22.    var _aspectRatio;
  23.    var dispatchEvent;
  24.    var addEventListener;
  25.    static var symbolName = "MediaDisplay";
  26.    static var symbolOwner = mx.controls.MediaDisplay;
  27.    var className = "MediaDisplay";
  28.    static var version = "2.0.0.360";
  29.    var clipParameters = {mediaType:"FLV",contentPath:"",totalTime:0,autoSize:true,autoPlay:true,initCuePointNames:new Array(),initCuePointTimes:new Array(),fps:30};
  30.    var _priorTime = 0;
  31.    var _priorBytesLoaded = 0;
  32.    var makeVideoVisible = false;
  33.    function MediaDisplay()
  34.    {
  35.       super();
  36.    }
  37.    function init(Void)
  38.    {
  39.       this.initializeParameters();
  40.       var _loc3_ = this._width;
  41.       var _loc4_ = this._height;
  42.       this._deadPreview._visible = false;
  43.       super.init();
  44.       this.setSize(_loc3_,_loc4_,true);
  45.       this.initCuePoints();
  46.       this._enabled = true;
  47.       this._playingBeforeDisabled = false;
  48.       this._started = false;
  49.       this._sendCompleteEvent = false;
  50.       this._fullyLoaded = false;
  51.       this.makeVideoVisible = false;
  52.       this.tabEnabled = false;
  53.       this.tabChildren = false;
  54.       if(this._contentPath != "")
  55.       {
  56.          this.initMedia();
  57.       }
  58.       this._screenAccommodator = new mx.controls.streamingmedia.ScreenAccommodator(this);
  59.    }
  60.    function initializeParameters()
  61.    {
  62.       if(this.mediaType == null)
  63.       {
  64.          this.mediaType = "FLV";
  65.       }
  66.       if(this.totalTime == null)
  67.       {
  68.          this.totalTime = 0;
  69.       }
  70.       if(this.contentPath == null)
  71.       {
  72.          this.contentPath = "";
  73.       }
  74.       if(this.autoPlay == null)
  75.       {
  76.          this.autoPlay = true;
  77.       }
  78.       if(this.autoSize == null)
  79.       {
  80.          this.autoSize = true;
  81.       }
  82.       if(this.aspectRatio == null)
  83.       {
  84.          this.aspectRatio = true;
  85.       }
  86.    }
  87.    function initCuePoints()
  88.    {
  89.       this._cuePoints = new Array();
  90.       var _loc2_ = 0;
  91.       while(_loc2_ < this.initCuePointNames.length && _loc2_ < this.initCuePointTimes.length)
  92.       {
  93.          this.addCuePoint(this.initCuePointNames[_loc2_],this.initCuePointTimes[_loc2_]);
  94.          _loc2_ = _loc2_ + 1;
  95.       }
  96.       delete this.initCuePointNames;
  97.       delete this.initCuePointTimes;
  98.    }
  99.    function initMedia(switchType)
  100.    {
  101.       if(this.isLivePreview())
  102.       {
  103.          return undefined;
  104.       }
  105.       if(switchType)
  106.       {
  107.          this._playerImpl.stop();
  108.       }
  109.       if(this.isFLV())
  110.       {
  111.          if(this.isRtmp(this._contentPath))
  112.          {
  113.             this._playerImpl = new mx.controls.streamingmedia.RTMPPlayer(this._contentPath,mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE,this._videoHolder,this._totalTime);
  114.          }
  115.          else
  116.          {
  117.             this._playerImpl = new mx.controls.streamingmedia.FLVPlayer(this._contentPath,this._videoHolder,this._totalTime);
  118.          }
  119.       }
  120.       else
  121.       {
  122.          if(!this.isMP3())
  123.          {
  124.             throw new Error("The display must be playing mp3 or flv media. Instead it is trying to play " + this._mediaType);
  125.          }
  126.          if(switchType)
  127.          {
  128.             this.releaseVideo();
  129.          }
  130.          if(this.isRtmp(this._contentPath))
  131.          {
  132.             this._playerImpl = new mx.controls.streamingmedia.RTMPPlayer(this._contentPath,mx.controls.streamingmedia.StreamingMediaConstants.MP3_MEDIA_TYPE,this._videoHolder,this._totalTime);
  133.          }
  134.          else
  135.          {
  136.             this.createEmptyMovieClip("_soundHolder",10);
  137.             this._playerImpl = new mx.controls.streamingmedia.MP3Player(this._contentPath,this._soundHolder);
  138.          }
  139.       }
  140.       this.redraw(true);
  141.       this._playerImpl.removeAllListeners();
  142.       this._playerImpl.addListener(this);
  143.       this._started = false;
  144.       if(!switchType)
  145.       {
  146.          if(this._autoPlay)
  147.          {
  148.             this.play();
  149.          }
  150.          else
  151.          {
  152.             this.load();
  153.          }
  154.          this.poll(true);
  155.          if(this._pollId != null)
  156.          {
  157.             clearInterval(this._pollId);
  158.          }
  159.          this._pollId = setInterval(this,"poll",250);
  160.       }
  161.    }
  162.    function draw()
  163.    {
  164.       if(this.isFLV())
  165.       {
  166.          if(this.makeVideoVisible)
  167.          {
  168.             this._videoHolder._visible = true;
  169.             this.makeVideoVisible = false;
  170.          }
  171.          if(this._autoSize)
  172.          {
  173.             this.assignPreferredSize();
  174.          }
  175.          else if(this._aspectRatio)
  176.          {
  177.             var _loc4_ = this.preferredWidth;
  178.             var _loc3_ = this.preferredHeight;
  179.             var _loc2_ = Math.min(this.width / _loc4_,this.height / _loc3_);
  180.             this.setVideoDisplaySize(_loc2_ * _loc4_,_loc2_ * _loc3_);
  181.          }
  182.          else
  183.          {
  184.             this.setVideoDisplaySize(this.width,this.height);
  185.          }
  186.       }
  187.    }
  188.    function setVideoDisplaySize(w, h)
  189.    {
  190.       if(this.isFLV())
  191.       {
  192.          var _loc2_ = this._videoHolder._video;
  193.          _loc2_._width = w;
  194.          _loc2_._height = h;
  195.          _loc2_._x = (this.width - _loc2_._width) / 2;
  196.          _loc2_._y = (this.height - _loc2_._height) / 2;
  197.       }
  198.    }
  199.    function get videoWidth()
  200.    {
  201.       var _loc2_ = undefined;
  202.       if(this.isMP3())
  203.       {
  204.          _loc2_ = 0;
  205.       }
  206.       else
  207.       {
  208.          _loc2_ = this._videoHolder._video._width;
  209.       }
  210.       return _loc2_;
  211.    }
  212.    function get videoHeight()
  213.    {
  214.       var _loc2_ = undefined;
  215.       if(this.isMP3())
  216.       {
  217.          _loc2_ = 0;
  218.       }
  219.       else
  220.       {
  221.          _loc2_ = this._videoHolder._video._height;
  222.       }
  223.       return _loc2_;
  224.    }
  225.    function get preferredWidth()
  226.    {
  227.       var _loc2_ = undefined;
  228.       if(this.isMP3())
  229.       {
  230.          _loc2_ = 0;
  231.       }
  232.       else
  233.       {
  234.          _loc2_ = this._videoHolder._video.width;
  235.       }
  236.       return _loc2_;
  237.    }
  238.    function get preferredHeight()
  239.    {
  240.       var _loc2_ = undefined;
  241.       if(this.isMP3())
  242.       {
  243.          _loc2_ = 0;
  244.       }
  245.       else
  246.       {
  247.          _loc2_ = this._videoHolder._video.height;
  248.       }
  249.       return _loc2_;
  250.    }
  251.    function assignPreferredSize()
  252.    {
  253.       this.setVideoDisplaySize(this.preferredWidth,this.preferredHeight,true);
  254.    }
  255.    function handlePlayer(player, status)
  256.    {
  257.       if(status == "start" && !this._started)
  258.       {
  259.          if(!(this._parent instanceof mx.controls.MediaPlayback))
  260.          {
  261.             this.makeVideoVisible = true;
  262.             this.draw();
  263.          }
  264.          var _loc2_ = {target:this,type:"start"};
  265.          this.dispatchEvent(_loc2_);
  266.          this._started = true;
  267.       }
  268.       else if(status == "complete")
  269.       {
  270.          this._sendCompleteEvent = true;
  271.       }
  272.    }
  273.    function toString()
  274.    {
  275.       return "MediaDisplay: media=" + this._contentPath;
  276.    }
  277.    function load()
  278.    {
  279.       this._playerImpl.load();
  280.    }
  281.    function play(startingPoint)
  282.    {
  283.       this._playerImpl.play(startingPoint);
  284.    }
  285.    function pause()
  286.    {
  287.       this._playerImpl.pause();
  288.    }
  289.    function stop()
  290.    {
  291.       this._playerImpl.stop();
  292.    }
  293.    function get autoSize()
  294.    {
  295.       return this._autoSize;
  296.    }
  297.    function set autoSize(flag)
  298.    {
  299.       if(this._autoSize != flag)
  300.       {
  301.          this._autoSize = flag;
  302.          this.invalidate();
  303.       }
  304.    }
  305.    function get aspectRatio()
  306.    {
  307.       return this._aspectRatio;
  308.    }
  309.    function set aspectRatio(flag)
  310.    {
  311.       if(this._aspectRatio != flag)
  312.       {
  313.          this._aspectRatio = flag;
  314.          this.invalidate();
  315.       }
  316.    }
  317.    function get autoPlay()
  318.    {
  319.       return this._autoPlay;
  320.    }
  321.    function set autoPlay(flag)
  322.    {
  323.       this._autoPlay = flag;
  324.    }
  325.    function get playheadTime()
  326.    {
  327.       return this._playerImpl.getPlayheadTime();
  328.    }
  329.    function set playheadTime(position)
  330.    {
  331.       this._playerImpl.setPlayheadTime(position);
  332.    }
  333.    function get contentPath()
  334.    {
  335.       return this._contentPath;
  336.    }
  337.    function set contentPath(aUrl)
  338.    {
  339.       this._contentPath = aUrl;
  340.    }
  341.    function setMedia(aUrl, aType)
  342.    {
  343.       if(aType != null)
  344.       {
  345.          if(aType != mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE && aType != mx.controls.streamingmedia.StreamingMediaConstants.MP3_MEDIA_TYPE)
  346.          {
  347.             throw new Error("The media type must be either " + mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE + " or " + mx.controls.streamingmedia.StreamingMediaConstants.MP3_MEDIA_TYPE);
  348.          }
  349.       }
  350.       else if(aType == null)
  351.       {
  352.          var _loc3_ = aUrl.substr(-3);
  353.          if(_loc3_ == "flv" || _loc3_ == "FLV")
  354.          {
  355.             aType = "FLV";
  356.          }
  357.          else if(_loc3_ == "mp3" || _loc3_ == "MP3")
  358.          {
  359.             aType = "MP3";
  360.          }
  361.          else
  362.          {
  363.             aType = this._mediaType;
  364.          }
  365.       }
  366.       var _loc7_ = this._mediaType;
  367.       this._mediaType = aType;
  368.       var _loc5_ = this.isRtmp(this._contentPath);
  369.       var _loc6_ = this.isRtmp(aUrl);
  370.       this._contentPath = aUrl;
  371.       this._fullyLoaded = false;
  372.       if(!this.isLivePreview())
  373.       {
  374.          if(this._contentPath == "")
  375.          {
  376.             this.releaseVideo();
  377.          }
  378.          else if(this._playerImpl == null)
  379.          {
  380.             this.initMedia();
  381.          }
  382.          else if(_loc7_ != this._mediaType || _loc5_ != _loc6_)
  383.          {
  384.             var _loc8_ = this._playerImpl.isPlaying();
  385.             this.initMedia(true);
  386.             if(_loc8_)
  387.             {
  388.                this._playerImpl.play(0);
  389.             }
  390.             else
  391.             {
  392.                this._playerImpl.load();
  393.             }
  394.          }
  395.          else
  396.          {
  397.             this._playerImpl.setMediaUrl(aUrl);
  398.             this._started = false;
  399.          }
  400.       }
  401.    }
  402.    function releaseVideo()
  403.    {
  404.       this._playerImpl.close();
  405.       this._playerImpl = null;
  406.    }
  407.    function isLivePreview()
  408.    {
  409.       return _root.contents.obj != null;
  410.    }
  411.    function get volume()
  412.    {
  413.       return this._playerImpl.getVolume();
  414.    }
  415.    function set volume(aVol)
  416.    {
  417.       this._playerImpl.setVolume(aVol);
  418.    }
  419.    function get playing()
  420.    {
  421.       return this._playerImpl.isPlaying();
  422.    }
  423.    function get bytesLoaded()
  424.    {
  425.       return this._playerImpl.getMediaBytesLoaded();
  426.    }
  427.    function get bytesTotal()
  428.    {
  429.       return this._playerImpl.getMediaBytesTotal();
  430.    }
  431.    function isFLV()
  432.    {
  433.       return this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE;
  434.    }
  435.    function isMP3()
  436.    {
  437.       return this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.MP3_MEDIA_TYPE;
  438.    }
  439.    function get mediaType()
  440.    {
  441.       return this._mediaType;
  442.    }
  443.    function set mediaType(aType)
  444.    {
  445.       this._mediaType = aType;
  446.    }
  447.    function get totalTime()
  448.    {
  449.       var _loc2_ = undefined;
  450.       if(this._playerImpl == null)
  451.       {
  452.          _loc2_ = this._totalTime;
  453.       }
  454.       else
  455.       {
  456.          _loc2_ = this._playerImpl.getTotalTime();
  457.       }
  458.       return _loc2_;
  459.    }
  460.    function set totalTime(aTime)
  461.    {
  462.       this._totalTime = aTime;
  463.       if(!(this._playerImpl instanceof mx.controls.streamingmedia.MP3Player))
  464.       {
  465.          mx.controls.streamingmedia.FLVPlayer(this._playerImpl).setTotalTime(aTime);
  466.       }
  467.    }
  468.    function getCuePoints()
  469.    {
  470.       return this._cuePoints;
  471.    }
  472.    function get cuePoints()
  473.    {
  474.       return this.getCuePoints();
  475.    }
  476.    function setCuePoints(cp)
  477.    {
  478.       this._cuePoints = cp;
  479.       var _loc2_ = 0;
  480.       while(_loc2_ < this._cuePoints.length)
  481.       {
  482.          this._cuePoints[_loc2_].display = this;
  483.          _loc2_ = _loc2_ + 1;
  484.       }
  485.    }
  486.    function set cuePoints(cp)
  487.    {
  488.       this.setCuePoints(cp);
  489.    }
  490.    function getCuePoint(pointName)
  491.    {
  492.       var _loc2_ = null;
  493.       var _loc3_ = this.getCuePointIndex(pointName);
  494.       if(_loc3_ > -1)
  495.       {
  496.          _loc2_ = this._cuePoints[_loc3_];
  497.       }
  498.       return _loc2_;
  499.    }
  500.    function addCuePoint(aName, aTime)
  501.    {
  502.       var _loc2_ = new mx.controls.streamingmedia.CuePoint(aName,aTime);
  503.       this.addCuePointObject(_loc2_);
  504.    }
  505.    function addCuePointObject(aCuePoint)
  506.    {
  507.       aCuePoint.display = this;
  508.       this._cuePoints.push(aCuePoint);
  509.    }
  510.    function removeCuePoint(aCuePoint)
  511.    {
  512.       var _loc2_ = this.getCuePointIndex(aCuePoint.name);
  513.       if(_loc2_ > -1)
  514.       {
  515.          this._cuePoints.splice(_loc2_,1);
  516.       }
  517.    }
  518.    function removeAllCuePoints()
  519.    {
  520.       this._cuePoints.length = 0;
  521.    }
  522.    function handleEvent(ev)
  523.    {
  524.       if(ev.type == "click" && ev.detail == "play")
  525.       {
  526.          this.handlePlayEvent(ev);
  527.       }
  528.       else if(ev.type == "click" && ev.detail == "pause")
  529.       {
  530.          this.handlePauseEvent(ev);
  531.       }
  532.       else if(ev.type == "playheadChange")
  533.       {
  534.          this.handlePlayheadChangeEvent(ev);
  535.       }
  536.       else if(ev.type == "volume")
  537.       {
  538.          this.handleVolumeEvent(ev);
  539.       }
  540.       else
  541.       {
  542.          this.handleUnrecognizedEvent(ev);
  543.       }
  544.    }
  545.    function handlePlayEvent(ev)
  546.    {
  547.       this.play();
  548.    }
  549.    function handleStopEvent(ev)
  550.    {
  551.       this.stop();
  552.    }
  553.    function handlePauseEvent(ev)
  554.    {
  555.       this.pause();
  556.    }
  557.    function handleRewindEvent(ev)
  558.    {
  559.       this.playheadTime = 0;
  560.    }
  561.    function handleFastForwardEvent(ev)
  562.    {
  563.       var _loc2_ = this.totalTime;
  564.       this.playheadTime = _loc2_;
  565.    }
  566.    function handlePlayheadChangeEvent(ev)
  567.    {
  568.       var _loc3_ = ev.detail;
  569.       var _loc2_ = _loc3_ / 100 * this.totalTime;
  570.       this.playheadTime = _loc2_;
  571.    }
  572.    function handleVolumeEvent(ev)
  573.    {
  574.       var _loc2_ = ev.detail;
  575.       this.volume = _loc2_;
  576.    }
  577.    function handleUnrecognizedEvent(ev)
  578.    {
  579.    }
  580.    function getCuePointIndex(pointName)
  581.    {
  582.       var _loc3_ = -1;
  583.       var _loc2_ = 0;
  584.       while(_loc2_ < this._cuePoints.length && _loc3_ == -1)
  585.       {
  586.          if(this._cuePoints[_loc2_].name == pointName)
  587.          {
  588.             _loc3_ = _loc2_;
  589.          }
  590.          _loc2_ = _loc2_ + 1;
  591.       }
  592.       return _loc3_;
  593.    }
  594.    function poll(first)
  595.    {
  596.       var _loc4_ = this.playheadTime;
  597.       var _loc6_ = this.bytesLoaded;
  598.       if(_loc4_ != this._priorTime)
  599.       {
  600.          if(this._mediaType == "MP3" && (this._priorTime == 0 || _loc4_ < 0.4) && !this._started)
  601.          {
  602.             var _loc7_ = {target:this,type:"start"};
  603.             this.dispatchEvent(_loc7_);
  604.             this._started = true;
  605.             mx.controls.streamingmedia.MP3Player(this._playerImpl).playStarted();
  606.          }
  607.          _loc7_ = {type:"change",target:this};
  608.          this.dispatchEvent(_loc7_);
  609.       }
  610.       var _loc5_ = false;
  611.       if(!this._fullyLoaded && this.bytesLoaded >= this.bytesTotal - 100)
  612.       {
  613.          this._fullyLoaded = true;
  614.          this._playerImpl.mediaLoaded();
  615.          _loc5_ = true;
  616.       }
  617.       else if(first || _loc6_ != this._priorBytesLoaded)
  618.       {
  619.          _loc5_ = true;
  620.       }
  621.       if(_loc5_)
  622.       {
  623.          _loc7_ = {type:"progress",target:this};
  624.          this.dispatchEvent(_loc7_);
  625.       }
  626.       var _loc2_ = null;
  627.       var _loc3_ = 0;
  628.       while(_loc3_ < this._cuePoints.length)
  629.       {
  630.          _loc2_ = this._cuePoints[_loc3_];
  631.          if(this._priorTime < _loc2_.time && _loc4_ >= _loc2_.time || this._priorTime > _loc2_.time && _loc4_ <= _loc2_.time)
  632.          {
  633.             _loc7_ = {type:"cuePoint",target:_loc2_};
  634.             this.dispatchEvent(_loc7_);
  635.          }
  636.          _loc3_ = _loc3_ + 1;
  637.       }
  638.       if(this._sendCompleteEvent)
  639.       {
  640.          this._sendCompleteEvent = false;
  641.          _loc7_ = {type:"complete",target:this};
  642.          this.dispatchEvent(_loc7_);
  643.       }
  644.       this._priorTime = _loc4_;
  645.       this._priorBytesLoaded = _loc6_;
  646.    }
  647.    function isRtmp(mediaUrl)
  648.    {
  649.       if(mediaUrl != null)
  650.       {
  651.          var _loc1_ = undefined;
  652.          _loc1_ = mediaUrl.toLowerCase();
  653.          return _loc1_.indexOf("rtmp") == 0;
  654.       }
  655.       return false;
  656.    }
  657.    function associateController(c)
  658.    {
  659.       if(this.isRtmp(this._contentPath))
  660.       {
  661.          c.setLoadBarFull();
  662.       }
  663.       c.addEventListener("click",this);
  664.       c.addEventListener("playheadChange",this);
  665.       c.addEventListener("volume",this);
  666.       this.addEventListener("change",c);
  667.       this.addEventListener("progress",c);
  668.       this.addEventListener("complete",c);
  669.    }
  670.    function setSize(w, h, noEvent)
  671.    {
  672.       super.setSize(w,h,noEvent);
  673.       this.invalidate();
  674.    }
  675.    function get enabled()
  676.    {
  677.       return this._enabled;
  678.    }
  679.    function set enabled(is)
  680.    {
  681.       if(this._enabled == is)
  682.       {
  683.          return;
  684.       }
  685.       this._enabled = is;
  686.       if(is)
  687.       {
  688.          if(this._playingBeforeDisabled)
  689.          {
  690.             this.play();
  691.             this._playingBeforeDisabled = false;
  692.          }
  693.       }
  694.       else
  695.       {
  696.          this._playingBeforeDisabled = this.playing;
  697.          if(this._playingBeforeDisabled && this._playerImpl instanceof mx.controls.streamingmedia.MP3Player)
  698.          {
  699.             this._playingBeforeDisabled = !mx.controls.streamingmedia.MP3Player(this._playerImpl).willStop();
  700.          }
  701.          this.pause();
  702.       }
  703.    }
  704.    function onUnload()
  705.    {
  706.       this._playerImpl.close();
  707.    }
  708. }
  709.