package com.exanimo.video { import flash.display.MovieClip; import fl.video.FLVPlayback; import flash.events.Event; /** * * Syncs a MovieClip to an FLVPlayback. * */ public class MovieClipSynchronizer { private var _startFrame:Number; private var _startTime:Number; public var movieClip:MovieClip; public var flvPlayback:FLVPlayback; public var accuracy:int = 3; // How many frames off do we allow it to get before sync'ing? public function MovieClipSynchronizer(movieClip:MovieClip, flvPlayback:FLVPlayback) { this.movieClip = movieClip; this.flvPlayback = flvPlayback; } /** * * Start syncing the MovieClip. * */ public function start():void { this._startFrame = this.movieClip.currentFrame; this._startTime = this.flvPlayback.playheadTime; this.movieClip.addEventListener(Event.ENTER_FRAME, this._sync); } /** * * Stop syncing the MovieClip. * */ public function stop():void { this.movieClip.removeEventListener(Event.ENTER_FRAME, this._sync); } /** * * Sync timeline to flv. * */ private function _sync(e:Event = null):void { var correctFrame:Number = this._startFrame + Math.floor((this.flvPlayback.playheadTime - this._startTime) * this.movieClip.stage.frameRate); var difference:Number = correctFrame - this.movieClip.currentFrame; // How off are we? if (difference > this.accuracy) { // The animation has fallen behind! Jump ahead to where the movie is. this.movieClip.gotoAndPlay(correctFrame); } else if (difference < -this.accuracy) { // The animation is too far along, fall back. this.movieClip.gotoAndPlay(correctFrame); } else if (difference < 0) { // The animation is ahead, but not by as many frames as we specified // in this.accuracy, wait for the video to catch up. this.movieClip.stop(); } else { this.movieClip.play(); } } } }