home *** CD-ROM | disk | FTP | other *** search
/ Neil's C++ Stuff / 2016-02-neilstuff-weebly.iso / apps / audioPlayer2.swf / scripts / __Packages / net / onepixelout / audio / Player.as next >
Encoding:
Text File  |  2016-02-05  |  8.4 KB  |  307 lines

  1. class net.onepixelout.audio.Player
  2. {
  3.    var _volume;
  4.    var _state;
  5.    var _clearID;
  6.    var _duration;
  7.    var _position;
  8.    var _loaded;
  9.    var _played;
  10.    var _isBuffering;
  11.    var _isConnecting;
  12.    var _recordedPosition;
  13.    var _startPlaying;
  14.    var _lastPosition;
  15.    var _playCounter;
  16.    var _playhead;
  17.    var _fadeVolume;
  18.    var _fadeClearID;
  19.    var broadcastMessage;
  20.    var _playlist;
  21.    var _delayID;
  22.    static var NOTFOUND = -1;
  23.    static var STOPPED = 1;
  24.    static var PAUSED = 2;
  25.    static var PLAYING = 3;
  26.    var _options = {playerID:"",initialVolume:60,enableCycling:true,syncVolumes:true,killDownload:true,checkPolicy:false,bufferTime:5};
  27.    function Player(options)
  28.    {
  29.       AsBroadcaster.initialize(this);
  30.       if(options != undefined)
  31.       {
  32.          this._setOptions(options);
  33.       }
  34.       this._volume = this._options.initialVolume;
  35.       this._state = net.onepixelout.audio.Player.STOPPED;
  36.       this._reset();
  37.       this._clearID = setInterval(this,"_watch",50);
  38.    }
  39.    function _setOptions(options)
  40.    {
  41.       for(var _loc3_ in options)
  42.       {
  43.          this._options[_loc3_] = options[_loc3_];
  44.       }
  45.    }
  46.    function _reset()
  47.    {
  48.       this._duration = 0;
  49.       this._position = 0;
  50.       this._loaded = 0;
  51.       this._played = 0;
  52.       this._isBuffering = false;
  53.       this._isConnecting = false;
  54.       this._recordedPosition = 0;
  55.       this._startPlaying = false;
  56.       this._lastPosition = 0;
  57.       this._playCounter = 0;
  58.    }
  59.    function play()
  60.    {
  61.       if(this._state == net.onepixelout.audio.Player.PLAYING)
  62.       {
  63.          return undefined;
  64.       }
  65.       this._setBufferTime(this._recordedPosition);
  66.       var _loc2_ = this.getCurrentTrack();
  67.       this._playhead = _loc2_.load(this._options.checkPolicy);
  68.       if(this._playhead.onSoundComplete == undefined)
  69.       {
  70.          this._playhead.onSoundComplete = mx.utils.Delegate.create(this,this.next);
  71.       }
  72.       if(this._state == net.onepixelout.audio.Player.STOPPED)
  73.       {
  74.          this._isConnecting = true;
  75.       }
  76.       this._state = net.onepixelout.audio.Player.PLAYING;
  77.       this.setVolume();
  78.       this._playhead.start(Math.floor(this._recordedPosition / 1000));
  79.       this._updateStats();
  80.    }
  81.    function pause()
  82.    {
  83.       if(this._state == net.onepixelout.audio.Player.PAUSED)
  84.       {
  85.          this.play();
  86.          return undefined;
  87.       }
  88.       if(this._state < net.onepixelout.audio.Player.PLAYING)
  89.       {
  90.          return undefined;
  91.       }
  92.       this._fadeVolume = this._volume;
  93.       this._fadeClearID = setInterval(this,"_fadeOut",50);
  94.       this._state = net.onepixelout.audio.Player.PAUSED;
  95.    }
  96.    function stop(broadcast)
  97.    {
  98.       if(broadcast == undefined)
  99.       {
  100.          broadcast = true;
  101.       }
  102.       if(broadcast)
  103.       {
  104.          this.broadcastMessage("onStop");
  105.       }
  106.       this._playhead.stop();
  107.       this.getCurrentTrack().unLoad();
  108.       this._playhead = null;
  109.       this._state = net.onepixelout.audio.Player.STOPPED;
  110.       this._reset();
  111.    }
  112.    function moveHead(newHeadPosition)
  113.    {
  114.       if(this._state < net.onepixelout.audio.Player.PAUSED)
  115.       {
  116.          return undefined;
  117.       }
  118.       var _loc2_ = this._duration * newHeadPosition;
  119.       if(this._state == net.onepixelout.audio.Player.PAUSED)
  120.       {
  121.          this._recordedPosition = _loc2_;
  122.       }
  123.       else
  124.       {
  125.          this._playhead.stop();
  126.          this._setBufferTime(_loc2_);
  127.          this._playhead.start(Math.floor(_loc2_ / 1000));
  128.       }
  129.       this._updateStats();
  130.    }
  131.    function next()
  132.    {
  133.       var _loc2_ = this._state == net.onepixelout.audio.Player.PLAYING || this._state == net.onepixelout.audio.Player.NOTFOUND;
  134.       if(this._playlist.next() != null && _loc2_)
  135.       {
  136.          this.stop(false);
  137.          this.play();
  138.       }
  139.       else
  140.       {
  141.          this.stop(true);
  142.       }
  143.    }
  144.    function previous()
  145.    {
  146.       var _loc2_ = this._state == net.onepixelout.audio.Player.PLAYING;
  147.       if(this._playlist.previous() != null && _loc2_)
  148.       {
  149.          this.stop(false);
  150.          this.play();
  151.       }
  152.       else
  153.       {
  154.          this.stop(false);
  155.       }
  156.    }
  157.    function setVolume(newVolume, broadcast)
  158.    {
  159.       clearInterval(this._delayID);
  160.       if(newVolume != undefined)
  161.       {
  162.          this._volume = newVolume;
  163.       }
  164.       if(this._state > net.onepixelout.audio.Player.STOPPED)
  165.       {
  166.          this._playhead.setVolume(this._volume);
  167.       }
  168.    }
  169.    function getState()
  170.    {
  171.       var _loc2_ = new Object();
  172.       _loc2_.state = this._state;
  173.       _loc2_.buffering = this._isBuffering;
  174.       _loc2_.connecting = this._isConnecting;
  175.       _loc2_.loaded = this._loaded;
  176.       _loc2_.played = this._played;
  177.       _loc2_.duration = this._duration;
  178.       _loc2_.position = this._position;
  179.       _loc2_.volume = this._volume;
  180.       _loc2_.trackIndex = this._playlist.getCurrentIndex();
  181.       _loc2_.hasNext = this._playlist.hasNext();
  182.       _loc2_.hasPrevious = this._playlist.hasPrevious();
  183.       _loc2_.trackCount = this._playlist.length;
  184.       _loc2_.trackInfo = this.getCurrentTrack().getInfo();
  185.       return _loc2_;
  186.    }
  187.    function _fadeOut()
  188.    {
  189.       this._fadeVolume -= 20;
  190.       if(this._fadeVolume <= 20)
  191.       {
  192.          clearInterval(this._fadeClearID);
  193.          this._recordedPosition = this._playhead.position;
  194.          this._playhead.stop();
  195.       }
  196.       else
  197.       {
  198.          this._playhead.setVolume(this._fadeVolume);
  199.       }
  200.    }
  201.    function _updateStats()
  202.    {
  203.       if(this._state > net.onepixelout.audio.Player.STOPPED && this._playhead.getBytesTotal() > 0)
  204.       {
  205.          this._isConnecting = false;
  206.          var _loc2_ = this.getCurrentTrack();
  207.          if(_loc2_.isFullyLoaded())
  208.          {
  209.             this._loaded = 1;
  210.             this._duration = this._playhead.duration;
  211.          }
  212.          else
  213.          {
  214.             this._loaded = this._playhead.getBytesLoaded() / this._playhead.getBytesTotal();
  215.             if(this._loaded == 1)
  216.             {
  217.                this._duration = this._playhead.duration;
  218.             }
  219.             else if(this._playhead.id3.TLEN != undefined)
  220.             {
  221.                this._duration = parseInt(this._playhead.id3.TLEN);
  222.             }
  223.             else
  224.             {
  225.                this._duration = 1 / this._loaded * this._playhead.duration;
  226.             }
  227.          }
  228.          if(this._playhead.position > 0)
  229.          {
  230.             this._position = this._playhead.position;
  231.             this._played = this._position / this._duration;
  232.          }
  233.          if(!_loc2_.isID3Loaded() && this._playhead.id3.songname.length > 0)
  234.          {
  235.             _loc2_.setInfo();
  236.          }
  237.       }
  238.    }
  239.    function _watch()
  240.    {
  241.       var _loc2_ = this.getCurrentTrack();
  242.       if(this._state > net.onepixelout.audio.Player.NOTFOUND && !_loc2_.exists())
  243.       {
  244.          this._reset();
  245.          this._state = net.onepixelout.audio.Player.NOTFOUND;
  246.          return undefined;
  247.       }
  248.       this._updateStats();
  249.       if(this._state == net.onepixelout.audio.Player.PLAYING)
  250.       {
  251.          if(++this._playCounter == 2)
  252.          {
  253.             this._playCounter = 0;
  254.             this._isBuffering = this._position == this._lastPosition;
  255.             this._lastPosition = this._position;
  256.          }
  257.       }
  258.    }
  259.    function isBuffering()
  260.    {
  261.       return this._isBuffering;
  262.    }
  263.    function isConnecting()
  264.    {
  265.       return this._isConnecting;
  266.    }
  267.    function _setBufferTime(newPosition)
  268.    {
  269.       if(this.getCurrentTrack().isFullyLoaded())
  270.       {
  271.          _root._soundbuftime = 0;
  272.          return undefined;
  273.       }
  274.       var _loc3_ = Math.round((this._loaded * this._duration - newPosition) / 1000);
  275.       if(_loc3_ >= this._options.bufferTime)
  276.       {
  277.          _root._soundbuftime = 0;
  278.       }
  279.       else
  280.       {
  281.          _root._soundbuftime = this._options.bufferTime - _loc3_;
  282.       }
  283.    }
  284.    function loadPlaylist(trackFileList, titleList, artistList)
  285.    {
  286.       if(titleList == undefined)
  287.       {
  288.          titleList = "";
  289.       }
  290.       if(artistList == undefined)
  291.       {
  292.          artistList = "";
  293.       }
  294.       this._playlist = new net.onepixelout.audio.Playlist(this._options.enableCycling);
  295.       this._playlist.loadFromList(trackFileList,titleList,artistList);
  296.       this._reset();
  297.    }
  298.    function getTrackCount()
  299.    {
  300.       return this._playlist.length;
  301.    }
  302.    function getCurrentTrack()
  303.    {
  304.       return this._playlist.getCurrent();
  305.    }
  306. }
  307.