如何获得一个声音对象,仍然加载最后长度是多少?如何获得、长度、加载、对象

2023-09-08 12:30:37 作者:弒血

我要创建一个基本的MP3播放器在ActionScript 3。我有一个基本的进度条,指示歌曲太多发挥。进度计算为百分比数0和1之间归为这样:

I'm creating a basic MP3 player in ActionScript 3. I have a basic progress bar that indicates how much of the song has played. The progress is calculated as a decimal percentage normalized between 0 and 1 as such:

var progress:Number = channel.position / sound.length;

的问题是,如果音频仍在加载/缓冲sound.length不正确。这使我的进度条跳过左右,甚至倒退行驶,直到声音已经完全加载,并且sound.length不再改变。

The problem is, if the audio is still loading/buffering the sound.length is incorrect. This causes my progress bar to skip around and even travel backwards until the sound has completely loaded and the sound.length isn't changing anymore.

什么是确定一个声音对象,仍然加载最后长度的最好方法是什么?

What is the best way to determine the final length of a sound object that is still loading?

推荐答案

有至少有两个选项:

1:留下你的进度栏为0%,不动它,直到声音已完全加载。那就是:

1: Leave your progress bar at 0%, and don't move it until the sound has loaded completely. That is:

sound.addEventListener(Event.COMPLETE, onSoundComplete);

private function onSoundComplete(event:Event):void {
    // Calculate progress
}

2:基于该已加载该文件的百分比近似百分比。事情是这样的:

2: Approximate percentage based on the percentage of the file that has already loaded. Something like this:

private var _sound:Sound = /* Your logic here */;
private var _channel:SoundChannel = _sound.play();

_sound.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);

private function onSoundProgress(event:ProgressEvent):void {
    var percentLoaded:Number = event.bytesLoaded / event.bytesTotal;
    var approxProgress:Number
        = _channel.position / _sound.length * percentLoaded;
    // Update your progress bar based on approxProgress
}