2009-11-18 90 views
0

我在閃光燈ActionScript 3中有一個播放器。我需要獲取聲音文件的總時間和聲音文件的當前時間。 我的代碼:獲取聲音的總時間和當前時間?

function onPlayProgress(evt:Event):void { 
      var sndLength:int = Math.ceil(snd.length /(snd.bytesLoaded/snd.bytesTotal)); 
      var seekbar = 100 * (channel.position/sndLength); 
      playBar.seekbar.x = seekbar*5.8; 
      var _totalTime:Number = (((snd.length /(snd.bytesLoaded/snd.bytesTotal))*0.001/60)); 

什麼是當前時間?

回答

3

我不是你所提供的代碼示例試圖傳達的信息完全清楚,但如果你想獲得良好的當前位置是在玩,你會做這樣的事情:

protected var sound:Sound; 
protected var soundChannel:SoundChannel; 

protected function loadSound():void 
{ 
    sound = new Sound(new URLRequest("path_to_sound.mp3")); 
    sound.addEventListener(Event.COMPLETE, onSoundLoadComplete); 
{ 

protected function onSoundLoadComplete(e:Event):void 
{ 
    sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete); 
    soundChannel = sound.play(); 
    addEventListener(Event.ENTER_FRAME, onEnterFrame); 
} 

//Calculuate the sound time 
protected function onEnterFrame(e:Event):void 
{ 
    var minutes:uint = Math.floor(soundChannel.position/1000/60); 
    var seconds:uint = Math.floor(soundChannel.position/1000) % 60; 
    trace('position: ' + minutes + ':' + seconds);   
}; 
+0

好的我得到了,但我怎麼能在一分鐘內轉換:( – coderex 2009-11-19 05:31:41

+0

謝謝verymuch – coderex 2009-11-19 16:07:29

+0

你如何獲得總時間? – coderex 2009-11-19 16:26:34

0

我不這麼認爲。 sound.length不能作爲總時間,它是加載流的總時間。如果你調試代碼,你會發現sound.length正在改變,直到音頻完全被加載。 但我不知道得到總時間... 我這樣計算, estimatedTotalTimeLength = sound.bytesTotal/sound.bytesLoaded * sound.length;