duration (Sound.duration プロパティ)

public duration : Number (読み取り専用)

ミリ秒数で示したサウンドの継続時間。

メモ : Flash Lite 2.0 でこのプロパティがサポートされるのは、Flash ネイティブサウンドのみです。ホストデバイスに固有のサウンド形式はサポートされません。

使用できるバージョン : ActionScript 1.0、Flash Lite 2.0

次の例では、サウンドをロードし、[出力] パネルにサウンドファイルの継続時間を表示します。次の ActionScript を FLA ファイルまたは AS ファイルに追加します。

var my_sound: Sound = new Sound();
my_sound.onLoad = function(success: Boolean) {
    var totalSeconds: Number = this.duration/1000;
    trace(this.duration+" ms ("+Math.round(totalSeconds)+" seconds)");
    var minutes: Number = Math.floor(totalSeconds/60);
    var seconds = Math.floor(totalSeconds)%60;
    if (seconds<10) {
    seconds = "0"+seconds;
    }
    trace(minutes+": "+seconds);
};
my_sound.loadSound("song1.mp3", true);

次の例では、SWF ファイルにいくつかの曲をロードします。Drawing API を使用して作成したプログレスバーは、ロードの進行状況を表示します。音楽が始まってロードが完了すると、[出力] パネルに情報が表示されます。音楽が始まってロードが完了すると、ログファイルに情報が書き込まれます。次の ActionScript を FLA ファイルまたは AS ファイルに追加します。

var pb_height: Number = 10;
var pb_width: Number = 100;
var pb: MovieClip = this.createEmptyMovieClip("progressBar_mc", this.getNextHighestDepth());
pb.createEmptyMovieClip("bar_mc", pb.getNextHighestDepth());
pb.createEmptyMovieClip("vBar_mc", pb.getNextHighestDepth());
pb.createEmptyMovieClip("stroke_mc", pb.getNextHighestDepth());
pb.createTextField("pos_txt", pb.getNextHighestDepth(), 0, pb_height, pb_width, 22);

pb._x = 100;
pb._y = 100;

with (pb.bar_mc) {
    beginFill(0x00FF00);
    moveTo(0, 0);
    lineTo(pb_width, 0);
    lineTo(pb_width, pb_height);
    lineTo(0, pb_height);
    lineTo(0, 0);
    endFill();
    _xscale = 0;
}
with (pb.vBar_mc) {
    lineStyle(1, 0x000000);
    moveTo(0, 0);
    lineTo(0, pb_height);
}
with (pb.stroke_mc) {
    lineStyle(3, 0x000000);
    moveTo(0, 0);
    lineTo(pb_width, 0);
    lineTo(pb_width, pb_height);
    lineTo(0, pb_height);
    lineTo(0, 0);
}

var my_interval: Number;
var my_sound: Sound = new Sound();
my_sound.onLoad = function(success: Boolean) {
    if (success) {
    trace("sound loaded");
    }
};
my_sound.onSoundComplete = function() {
    clearInterval(my_interval);
    trace("Cleared interval");
}
my_sound.loadSound("song3.mp3", true);
my_interval = setInterval(updateProgressBar, 100, my_sound);

function updateProgressBar(the_sound: Sound): Void {
    var pos: Number = Math.round(the_sound.position/the_sound.duration 100);
    pb.bar_mc._xscale = pos;
    pb.vBar_mc._x = pb.bar_mc._width;
    pb.pos_txt.text = pos+"%";
}

関連項目

position (Sound.position プロパティ)