3 Replies Latest reply on Oct 16, 2009 6:59 AM by kglad

    shuffling music player

    xranerx

      I searched the site and google to see if i could find coding that would work, but it doesnt seem to be suited to the way my script is set up.  Heres the code as it is, can someone show me what could be changed to make it shuffle the song?  Thanks a lot

       

       

      class as.Music {
      private var _data:FastXML;
      private var _sound:Sound;
      private var _defaultVolume:Number;
      private var _volume:Number;
      private var _loopMode:Number = 0;
      private var _current_index:Number = -1;
      private var _mute:Boolean = false;
      private var _intervalProgressID:Number;

      private var broadcastMessage:Function;
      public var addListener:Function;
      public var removeListener:Function;
      private var _listeners:Array;

      public function Music(data:FastXML) {
        AsBroadcaster.initialize(this);
        _data = data;
       
        _defaultVolume = Number(_data.attributes.volum) || 100;
        _loopMode = Number(_data.attributes.loop_mode) || _loopMode;
        _sound = new Sound();
        setVolume(_defaultVolume, false);
        _sound.onLoad = Delegate.create(this, _onLoadHandler);
        _sound.onID3 = Delegate.create(this, _onID3Handler);
        _sound.onSoundComplete = Delegate.create(this, _onSoundCompleteHandler);
        next();
      }

      public function next():Void {
        _current_index = Calc.inIntervalRound(0, _data.item.length - 1, _current_index + 1);
        _loadSong(_data.item[_current_index]);
      }

      public function prev():Void {
        _current_index = Calc.inIntervalRound(0, _data.item.length - 1, _current_index - 1);
        _loadSong(_data.item[_current_index]);
      }

      public function get mute():Boolean { return _mute; }
      public function set mute(value:Boolean):Void { setMute(value); }
      public function setMute(value:Boolean, soft:Boolean):Void {
        if (!_mute == value) {
          setVolume((value)?0:_defaultVolume, true);
         _mute = value;
        }
      }

      public function get volume():Number { return _sound.getVolume(); }
      public function set volume(value:Number):Void { setVolume(value); }
      public function setVolume(value:Number, soft:Boolean):Void {
        value = Calc.inInterval(0, 100, value);
        if (soft){
         var tween:TweenProperties = new TweenProperties(_root, Regular.easeOut, volume, value, 1000);
         tween.onTweenUpdate = Delegate.create(this, setVolume);
         tween.endFunc = Delegate.create(this, _setedVolume)
         return;
        } else {
         _volume = value;
        }
        _sound.setVolume(value);
      }

      private function _loadSong(info:FastXML):Void {
        _sound.loadSound(info.value(), !Boolean(info.attributes.preload));
        broadcastMessage('onLoadStart', this);
        clearInterval(_intervalProgressID);
        _intervalProgressID = setInterval(this, '_checkProgressLoadingHandler', 30);
      }

      private function _checkProgressLoadingHandler():Void {
        var bytesLoaded:Number = _sound.getBytesLoaded() || 0;
        var bytesTotal:Number = _sound.getBytesTotal() || 0;
        if (bytesLoaded == bytesTotal) {
         clearInterval(_intervalProgressID);
         broadcastMessage('onLoadComplete', this);
        } else {
         broadcastMessage('onLoadProgress', this, bytesLoaded, bytesTotal);
        }
      }

      private function _setedVolume(ttt):Void {
        trace(ttt);
        _volume = _sound.getVolume();
      }

      private function _onLoadHandler(success:Boolean):Void {
        if (success) {
         _sound.start();
         setVolume(_volume);
        } else {
         next();
        }
      }

      private function _onID3Handler():Void {
        broadcastMessage('onID3', this, _sound);
      }

      private function _onSoundCompleteHandler():Void {
        broadcastMessage('onSoundComplete', this);
        switch (_loopMode) {
         case 0: // loop current
          _sound.start(0);
         break;
         
         case 1:// loop all
          next();
         break;
        
         case 2: // no loop
         
         break;
        }