2011-01-20 135 views
0

我試圖更新播放器欄作爲MP3播放。該欄應該代表歌曲的長度並更新以顯示迄今爲止播放了多少首歌曲。更新MP3播放器欄

有點丟失bytesLoaded bytesTotal或?

假設條的寬度爲200px;

我試過將這個和那個分開。沒有運氣

回答

1

在這裏,你去。

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.media.Sound; 
    import flash.media.SoundChannel; 
    import flash.net.URLRequest; 
    public class Engine extends Sprite 
    { 
     private var _sound:Sound; 
     private var _soundBar:Sprite; 
     private var _soundChannel:SoundChannel; 
     public function Engine() 
     { 
      addSoundChannel(); 
      drawSoundBar(); 
      //load sound 
      loadSound("sound.mp3"); 
     } 

     private function addSoundChannel():void 
     { 
      _soundChannel = new SoundChannel(); 
     } 

     private function drawSoundBar():void 
     { 
      //draw box 200px x 10px with a red fill 
      _soundBar = new Sprite(); 
      _soundBar.graphics.beginFill(0xFF0000); 
      _soundBar.graphics.drawRect(0, 0, 200, 10); 
      _soundBar.graphics.endFill(); 
      addChild(_soundBar); 
      _soundBar.x = (stage.stageWidth/2) - (_soundBar.width/2); 
      _soundBar.y = (stage.stageHeight/2) - (_soundBar.height/2); 
     } 

     private function loadSound(url:String):void 
     { 
      var toLoad:URLRequest = new URLRequest(url); 
      _sound = new Sound(); 
      _sound.load(toLoad); 
      _sound.addEventListener(Event.COMPLETE, soundLoaded, false, 0, true); 
     } 

     private function soundLoaded(evt:Event):void 
     { 
      trace("loaded"); 
      _soundChannel = _sound.play(); 
      addListeners(); 
     } 

     private function addListeners():void 
     { 
      stage.addEventListener(Event.ENTER_FRAME, checkSound, false, 0, true); 
      _soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true); 
     } 

     private function checkSound(evt:Event):void 
     { 
      //adjust scaleX from 0 to 1 based on sound position 
      _soundBar.scaleX = _soundChannel.position/_sound.length; 
     } 

     private function soundComplete(evt:Event):void 
     { 
      trace("done"); 
      stage.removeEventListener(Event.ENTER_FRAME, checkSound); 
     } 

    } 

} 

SoundChannel.position抓住以毫秒爲單位的當前位置,同時Sound.length是以毫秒爲單位的聲音的長度。

+0

只要給他的代碼不會幫助 – 2011-01-20 03:21:55