Get sound's total time and current time?

2019-08-23 06:35发布

问题:

I have a player in flash ActionScript 3. I need to take a the total time of the sound file and current time of the sound file. My code:

function onPlayProgress(evt:Event):void {
            var sndLength:int = Math.ceil(snd.length /(snd.bytesLoaded / snd.bytesTotal));
            var seekbar = 100 * (channel.position / sndLength);
            playBar.seekbar.x = seekbar*5.8;
            var _totalTime:Number = (((snd.length /(snd.bytesLoaded / snd.bytesTotal))*0.001/60));

What is current time?

回答1:

I'm not entirely clear on what the code sample you provided is trying to communicate, but if you want to get the current position of a sound that's playing you would do something like this:

protected var sound:Sound; 
protected var soundChannel:SoundChannel;

protected function loadSound():void
{
    sound = new Sound(new URLRequest("path_to_sound.mp3"));
    sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
{

protected function onSoundLoadComplete(e:Event):void
{
    sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
    soundChannel = sound.play();
    addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

//Calculuate the sound time
protected function onEnterFrame(e:Event):void
{
    var minutes:uint = Math.floor(soundChannel.position / 1000 / 60);
    var seconds:uint = Math.floor(soundChannel.position / 1000) % 60;
    trace('position: ' + minutes + ':' + seconds);        
};


回答2:

I don't think so. sound.length cannot be taken as the total time, it is the total time of the loaded stream. If you debug the code, you'll find the sound.length is changing untill the audio is totally loadded. But I have no idea to get the total time yet... I calculate it like this, estimatedTotalTimeLength = sound.bytesTotal/sound.bytesLoaded * sound.length;