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

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

问题描述

在一个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
   ...

有没有人注意到了这种行为?有什么方法来确定哪些框架的音频正在播放,知道这inacuracy?

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

推荐答案

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

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.

不过,为了更准确,你可以尝试使用Timer类。通常情况下,可以使蜱发生比你的帧速度更快,这意味着犯错的余地会更低。但是,你正在使用的事件,因此可能有一些漂移。

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 Acuracy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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