15.6.获得声音文件的大小

问题
我想知道一个mp3文件的大小及当前载入的大小。
解决办法
通过Sound对象的bytesTotal和bytesLoaded属性
讨论
当载入音频文件时,最好能让用户看到当前载入数据的进度。最后有一个可视化的进度条,就像Windows Media Player或QuickTime Player那样。

这一节我们利用Sound对象的两个属性做个灰色的进度条,bytesTotal和bytesLoaded.。bytesTotal指当前播放的mp3文件的总大小,bytesLoaded指已经下载的数据大小,两者相除即为声音下载的百分比。

下面的例子建立一个enterFrame监听器,每帧画一次白色矩形作为声音总大小,在计算出下载量画一个灰色的矩形。

+展开
-ActionScript
package {
import flash.display.Sprite;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.Event;
public class ProgressBar extends Sprite {
private var _sound:Sound;
public function ProgressBar( ) {
addEventListener(Event.ENTER_FRAME, onEnterFrame);
_sound = new Sound(new URLRequest("song.mp3"));
_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;
if(total > 0) {
// Draw a background bar
graphics.clear( );
graphics.beginFill(0xFFFFFF);
graphics.drawRect(10, 10, barWidth, barHeight);
graphics.endFill( );
// The percent of the sound that has loaded
var percent:Number = loaded / total;
// Draw a bar that represents the percent of
// the sound that has loaded
graphics.beginFill(0xCCCCCC);
graphics.drawRect(10, 10,
barWidth * percent, barHeight);
graphics.endFill( );
}
}
}
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(81)喜欢(0)flash/flex/fcs/AIR