home *** CD-ROM | disk | FTP | other *** search
- class net.onepixelout.audio.Player
- {
- var _volume;
- var _state;
- var _clearID;
- var _duration;
- var _position;
- var _loaded;
- var _played;
- var _isBuffering;
- var _isConnecting;
- var _recordedPosition;
- var _startPlaying;
- var _lastPosition;
- var _playCounter;
- var _playhead;
- var _fadeVolume;
- var _fadeClearID;
- var broadcastMessage;
- var _playlist;
- var _delayID;
- static var NOTFOUND = -1;
- static var STOPPED = 1;
- static var PAUSED = 2;
- static var PLAYING = 3;
- var _options = {playerID:"",initialVolume:60,enableCycling:true,syncVolumes:true,killDownload:true,checkPolicy:false,bufferTime:5};
- function Player(options)
- {
- AsBroadcaster.initialize(this);
- if(options != undefined)
- {
- this._setOptions(options);
- }
- this._volume = this._options.initialVolume;
- this._state = net.onepixelout.audio.Player.STOPPED;
- this._reset();
- this._clearID = setInterval(this,"_watch",50);
- }
- function _setOptions(options)
- {
- for(var _loc3_ in options)
- {
- this._options[_loc3_] = options[_loc3_];
- }
- }
- function _reset()
- {
- this._duration = 0;
- this._position = 0;
- this._loaded = 0;
- this._played = 0;
- this._isBuffering = false;
- this._isConnecting = false;
- this._recordedPosition = 0;
- this._startPlaying = false;
- this._lastPosition = 0;
- this._playCounter = 0;
- }
- function play()
- {
- if(this._state == net.onepixelout.audio.Player.PLAYING)
- {
- return undefined;
- }
- this._setBufferTime(this._recordedPosition);
- var _loc2_ = this.getCurrentTrack();
- this._playhead = _loc2_.load(this._options.checkPolicy);
- if(this._playhead.onSoundComplete == undefined)
- {
- this._playhead.onSoundComplete = mx.utils.Delegate.create(this,this.next);
- }
- if(this._state == net.onepixelout.audio.Player.STOPPED)
- {
- this._isConnecting = true;
- }
- this._state = net.onepixelout.audio.Player.PLAYING;
- this.setVolume();
- this._playhead.start(Math.floor(this._recordedPosition / 1000));
- this._updateStats();
- }
- function pause()
- {
- if(this._state == net.onepixelout.audio.Player.PAUSED)
- {
- this.play();
- return undefined;
- }
- if(this._state < net.onepixelout.audio.Player.PLAYING)
- {
- return undefined;
- }
- this._fadeVolume = this._volume;
- this._fadeClearID = setInterval(this,"_fadeOut",50);
- this._state = net.onepixelout.audio.Player.PAUSED;
- }
- function stop(broadcast)
- {
- if(broadcast == undefined)
- {
- broadcast = true;
- }
- if(broadcast)
- {
- this.broadcastMessage("onStop");
- }
- this._playhead.stop();
- this.getCurrentTrack().unLoad();
- this._playhead = null;
- this._state = net.onepixelout.audio.Player.STOPPED;
- this._reset();
- }
- function moveHead(newHeadPosition)
- {
- if(this._state < net.onepixelout.audio.Player.PAUSED)
- {
- return undefined;
- }
- var _loc2_ = this._duration * newHeadPosition;
- if(this._state == net.onepixelout.audio.Player.PAUSED)
- {
- this._recordedPosition = _loc2_;
- }
- else
- {
- this._playhead.stop();
- this._setBufferTime(_loc2_);
- this._playhead.start(Math.floor(_loc2_ / 1000));
- }
- this._updateStats();
- }
- function next()
- {
- var _loc2_ = this._state == net.onepixelout.audio.Player.PLAYING || this._state == net.onepixelout.audio.Player.NOTFOUND;
- if(this._playlist.next() != null && _loc2_)
- {
- this.stop(false);
- this.play();
- }
- else
- {
- this.stop(true);
- }
- }
- function previous()
- {
- var _loc2_ = this._state == net.onepixelout.audio.Player.PLAYING;
- if(this._playlist.previous() != null && _loc2_)
- {
- this.stop(false);
- this.play();
- }
- else
- {
- this.stop(false);
- }
- }
- function setVolume(newVolume, broadcast)
- {
- clearInterval(this._delayID);
- if(newVolume != undefined)
- {
- this._volume = newVolume;
- }
- if(this._state > net.onepixelout.audio.Player.STOPPED)
- {
- this._playhead.setVolume(this._volume);
- }
- }
- function getState()
- {
- var _loc2_ = new Object();
- _loc2_.state = this._state;
- _loc2_.buffering = this._isBuffering;
- _loc2_.connecting = this._isConnecting;
- _loc2_.loaded = this._loaded;
- _loc2_.played = this._played;
- _loc2_.duration = this._duration;
- _loc2_.position = this._position;
- _loc2_.volume = this._volume;
- _loc2_.trackIndex = this._playlist.getCurrentIndex();
- _loc2_.hasNext = this._playlist.hasNext();
- _loc2_.hasPrevious = this._playlist.hasPrevious();
- _loc2_.trackCount = this._playlist.length;
- _loc2_.trackInfo = this.getCurrentTrack().getInfo();
- return _loc2_;
- }
- function _fadeOut()
- {
- this._fadeVolume -= 20;
- if(this._fadeVolume <= 20)
- {
- clearInterval(this._fadeClearID);
- this._recordedPosition = this._playhead.position;
- this._playhead.stop();
- }
- else
- {
- this._playhead.setVolume(this._fadeVolume);
- }
- }
- function _updateStats()
- {
- if(this._state > net.onepixelout.audio.Player.STOPPED && this._playhead.getBytesTotal() > 0)
- {
- this._isConnecting = false;
- var _loc2_ = this.getCurrentTrack();
- if(_loc2_.isFullyLoaded())
- {
- this._loaded = 1;
- this._duration = this._playhead.duration;
- }
- else
- {
- this._loaded = this._playhead.getBytesLoaded() / this._playhead.getBytesTotal();
- if(this._loaded == 1)
- {
- this._duration = this._playhead.duration;
- }
- else if(this._playhead.id3.TLEN != undefined)
- {
- this._duration = parseInt(this._playhead.id3.TLEN);
- }
- else
- {
- this._duration = 1 / this._loaded * this._playhead.duration;
- }
- }
- if(this._playhead.position > 0)
- {
- this._position = this._playhead.position;
- this._played = this._position / this._duration;
- }
- if(!_loc2_.isID3Loaded() && this._playhead.id3.songname.length > 0)
- {
- _loc2_.setInfo();
- }
- }
- }
- function _watch()
- {
- var _loc2_ = this.getCurrentTrack();
- if(this._state > net.onepixelout.audio.Player.NOTFOUND && !_loc2_.exists())
- {
- this._reset();
- this._state = net.onepixelout.audio.Player.NOTFOUND;
- return undefined;
- }
- this._updateStats();
- if(this._state == net.onepixelout.audio.Player.PLAYING)
- {
- if(++this._playCounter == 2)
- {
- this._playCounter = 0;
- this._isBuffering = this._position == this._lastPosition;
- this._lastPosition = this._position;
- }
- }
- }
- function isBuffering()
- {
- return this._isBuffering;
- }
- function isConnecting()
- {
- return this._isConnecting;
- }
- function _setBufferTime(newPosition)
- {
- if(this.getCurrentTrack().isFullyLoaded())
- {
- _root._soundbuftime = 0;
- return undefined;
- }
- var _loc3_ = Math.round((this._loaded * this._duration - newPosition) / 1000);
- if(_loc3_ >= this._options.bufferTime)
- {
- _root._soundbuftime = 0;
- }
- else
- {
- _root._soundbuftime = this._options.bufferTime - _loc3_;
- }
- }
- function loadPlaylist(trackFileList, titleList, artistList)
- {
- if(titleList == undefined)
- {
- titleList = "";
- }
- if(artistList == undefined)
- {
- artistList = "";
- }
- this._playlist = new net.onepixelout.audio.Playlist(this._options.enableCycling);
- this._playlist.loadFromList(trackFileList,titleList,artistList);
- this._reset();
- }
- function getTrackCount()
- {
- return this._playlist.length;
- }
- function getCurrentTrack()
- {
- return this._playlist.getCurrent();
- }
- }
-