How to get the final length on a Sound object that is still loading?

大憨熊 提交于 2020-01-01 10:50:58

问题


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;

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:


There are at least two options:

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: 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
}



回答2:


You can also find out the length of the sound via its ID3 tag if the sound has been properly tagged.



来源:https://stackoverflow.com/questions/669974/how-to-get-the-final-length-on-a-sound-object-that-is-still-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!