如何获得一个声音对象,仍然加载最后长度是多少? [英] How to get the final length on a Sound object that is still loading?

查看:127
本文介绍了如何获得一个声音对象,仍然加载最后长度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个基本的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
}

这篇关于如何获得一个声音对象,仍然加载最后长度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆