Get sound's total time and current time?

谁都会走 提交于 2019-12-25 01:44:16

问题


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;



来源:https://stackoverflow.com/questions/1758752/get-sounds-total-time-and-current-time

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