帧同步音频和channel.position精度 [英] Syncing Frames to Audio and channel.position Accuracy

查看:189
本文介绍了帧同步音频和channel.position精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ENTER_FRAME事件调用channel.position,我注意到,它没有被更新的每一帧,但看起来更像是每帧半。

Calling channel.position on an ENTER_FRAME event, I notice that it's not being updated every frame, but it looks more like every frame and a half.

var sound:Sound = new Sound(new URLRequest('music.mp3')); 
var channel:SoundChannel = sound.play(); // assume the sound is completely,
                                         // totally, 100% loaded

addEventListener(Event.ENTER_FRAME, function(e:Event):void{
   trace(  "Position : " + channel.position 
         + " - Frame : " + int(channel.position / 30));
});

将导致沿东西线(30 FPS)

will result in something along the lines of (30 FPS)

   ...
   Position : 1439.6371882086166 - Frame : 47
   // 48 is missing
** Position : 1486.077097505669 -  Frame : 49
** Position : 1486.077097505669 -  Frame : 49
   Position : 1532.517006802721 -  Frame : 51
   Position : 1578.9569160997733 - Frame : 52
   // 53 is missing
** Position : 1625.3968253968253 - Frame : 54
** Position : 1625.3968253968253 - Frame : 54
   Position : 1671.8367346938776 - Frame : 55
   // 56 is missing
   Position : 1718.2766439909296 - Frame : 57
   ...

有没有人注意到了这种行为?是否有任何技术确定音频正在播放该框架,知道这不准确?

Has anyone noticed this behavior before? Are there any techniques for determining which 'frame' of audio is being played, knowing this inaccuracy?

推荐答案

是的,这是正常的行为,因为事件线程,因此将称他们代表自己的时候线程的优先级。打印到控制台还线程,因此它可能不总是在正确的时间打印消息无论是。逸岸,你看到的问题可能只是一个打印问题。尽量提高你的帧率,看看会发生什么。

Yes, this is normal behaviour because events are threaded and therefore will call their delegates whenever their thread has priority. Printing to the console is also threaded so it may not always be printing messages at the right time either. Infact, the issue you're seeing is probably just a printing issue. Try boosting your framerate, see what happens.

不过,为了更准确,你可以尝试使用定时器类。通常情况下,可以使蜱发生比你的框架要快得多,这意味着出错的利润率会较低。尽管如此,您使用的事件,因此可能会有一些偏差。

Still, in order to be more accurate you could try using the timer class. Typically, you can make the ticks happen much faster than your frames, meaning the margin for error will be lower. Still, you're using the event so there may be some drift.

要弥补这一点,你可以检查时间与帧来确定偏移。这使您可以校正任何漂移。

To compensate for this, you can check the time versus the frames to determine the offset. This allow you to correct for any drift.

编辑:这个例子是直接从<一把拉住href=\"http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/media/SoundChannel.html#includeExamplesSummary\"相对=nofollow>中的ActionScript 3文档在这个页面,注意 positionTimer 他们正在使用:

This example was pulled straight from this page in the ActionScript 3 documentation, notice the positionTimer they're using:

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.Timer;

    public class SoundChannelExample extends Sprite {
        private var url:String = "MySound.mp3";
        private var soundFactory:Sound;
        private var channel:SoundChannel;
        private var positionTimer:Timer;

        public function SoundChannelExample() {
            var request:URLRequest = new URLRequest(url);
            soundFactory = new Sound();
            soundFactory.addEventListener(Event.COMPLETE, completeHandler);
            soundFactory.addEventListener(Event.ID3, id3Handler);
            soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            soundFactory.load(request);

            channel = soundFactory.play();
            channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);

            positionTimer = new Timer(50);
            positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
            positionTimer.start();
        }


        private function positionTimerHandler(event:TimerEvent):void {
            trace("positionTimerHandler: " + channel.position.toFixed(2));
        }

        private function completeHandler(event:Event):void {
            trace("completeHandler: " + event);
        }

        private function id3Handler(event:Event):void {
            trace("id3Handler: " + event);
        }

        private function ioErrorHandler(event:Event):void {
            trace("ioErrorHandler: " + event);
            positionTimer.stop();       
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler: " + event);
        }

        private function soundCompleteHandler(event:Event):void {
            trace("soundCompleteHandler: " + event);
            positionTimer.stop();
        }
    }
}

这篇关于帧同步音频和channel.position精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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