coderex
coderex

Reputation: 27855

Get sound's total time and current time?

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?

Upvotes: 0

Views: 8228

Answers (2)

user2955639
user2955639

Reputation: 1

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;

Upvotes: 0

heavilyinvolved
heavilyinvolved

Reputation: 1575

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);        
};

Upvotes: 3

Related Questions