我們想知道目前的歌曲已經播放了百分之幾(意譯)
解決方法
用Sound.length擷取聲音檔案的長度,用SoundChannel.position擷取目前播放了多少。
具體讨論成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
問題六[原創]ActionScript3 使用sound類(e文翻譯)——擷取檔案大小中,我們提到了如何添加一個不僅顯示下載下傳進度還顯示播放進度的進度條,那一節中學習了建立顯示下載下傳進度的部分。
成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
本節讨論另外一個部分:如何跟蹤播放進度。為了實作這個目标,我們必須知道兩個量:目前檔案播放位置是多少和檔案總長度。雖然它們看起來差不多,但是它們卻在不同的類中,檔案長度資訊是sound對象的一個屬性,目前播放位置在SoundChannel類中。類似于問題六[原創]ActionScript3 使用sound類(e文翻譯)——擷取檔案大小中建立已下載下傳進度條一樣,有這兩個量很容易得到播放進度條。
成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
不幸的是,播放進度條做起來比下載下傳進度條要複雜,因為聲音檔案的總長度要等到聲音檔案全部下載下傳完畢之後才能得到,此時讀取檔案長度隻是得到了已經下載下傳的聲音檔案的長度。是以,比如:以個十分鐘的音樂,下載下傳了10%,它的長度(length)是一分鐘。(事實上,length和position都傳回毫秒為機關的資料,如果需要,我們也可以把它轉換成“分:秒”的表示形式)
成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
幸運的是,我們隻要經過一點小小的數學運算就能得到檔案的總長度(毫秒),我們拿目前檔案的長度(毫秒)除以目前已經下載下傳下來的百分比,結果将非常接近檔案的實際長度(毫秒)。說一下剛剛提到的例子:長度傳回值是1分鐘,這個一分鐘是指已經下載下傳的檔案長度。當我們拿這個一分鐘除以1/10,就能得到結果是10,這樣我們就得到了檔案的總長度是10分鐘。
成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
上面提到的百分比是用bytesLoaded/bytesTotal得到的,這個值是我們在做下載下傳進度條時候用過并存起來的,是以是以檔案總長度隻要一行代碼就能實作了:
成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
length /= percentBuffered;
成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
說明:問題六[原創]ActionScript3 使用sound類(e文翻譯)——擷取檔案大小中,我們提到了要想正确顯示進度條要清空緩存的問題,在此不再重複。
成業原創,轉載說明出處 http://hi.baidu.com/flash_as3
下面的代碼展示了如何把兩個進度條放在一起:
package {
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
public class ProgressBar2 extends Sprite {
private var _sound:Sound;
private var _channel:SoundChannel;
public function ProgressBar2( ) {
addEventListener(Event.ENTER_FRAME, onEnterFrame);
_sound = new Sound(new URLRequest("song.mp3"));
_channel = _sound.play( );
}
public function onEnterFrame(event:Event):void{
var barWidth:int = 200;
var barHeight:int = 5;
var loaded:int = _sound.bytesLoaded;
var total:int = _sound.bytesTotal;
var length:int = _sound.length;
var position:int = _channel.position;
// Draw a background bar
graphics.clear( );
graphics.beginFill(0xFFFFFF);
graphics.drawRect(10, 10, barWidth, barHeight);
graphics.endFill( );
if(total > 0) {
// The percent of the sound that has loaded
var percentBuffered:Number = loaded / total;
// Draw a bar that represents the percent of
// the sound that has loaded
graphics.beginFill(0xCCCCCC);
graphics.drawRect(10, 10,
barWidth * percentBuffered,
barHeight);
graphics.endFill( );
// Correct the sound length calculation
length /= percentBuffered;
// The percent of the sound that has played
var percentPlayed:Number = position / length;
// Draw a bar that represents the percent of
// the sound that has played
graphics.beginFill(0x666666);
graphics.drawRect(10, 10,
barWidth * percentPlayed,
barHeight);
graphics.endFill( );
}
}
}
}
譯者注:上面的這個例子的使用方法:
1,建立立一個fla檔案,命名,儲存;
2,找首歌曲(mp3格式)複制到fla的那個目錄裡面,命名為song.mp3(mp3是字尾名);
3,建立一個.as檔案,把上述代碼複制到其中,儲存在fla同一個目錄,命名為ProgressBar2.as;
4,把fla檔案的document class屬性(選中舞台,打開屬性面闆就能看到)設定為ProgressBar2,設定背景顔色為蘭色(隻要不是白色黑色灰色就行);
5,按ctrl+enter測試。我們看到效果。
如果看不到加載進度,請換個網絡上的歌曲試試看。