I’m currently working on a project where I have been handed a lot of movieclips with animated mouths to be sync-ed with spoken audio triggered from the library. Each sentence had been put together in a separate movieclip. Obviously this method exposes the frame rate on slow computers leading to loss of lip-synchronisation. I was told to go through all of the moveiclips and add the audio to the timeline – a very inelegant solution that would take a lot of time with poor results.
I came up with a simple solution that basically just pushes the playhead forward everytime it drops out of sync. I use the frame rate and getTimer to work out where the playhead should be on each iteration. If the playhead drops then it is moved to the position it should be at.
Very simple – saved me a lot of time.
private const FPS:int = 30//Current frame rate
private const ERROR_MARGIN:int=1
private var start_time:int
private var last_frame:int=0//Keep track of last frame to see if mc has stopped playing
private var loop_started:Boolean=falsepublic function sayPhrase(phrase:String) : void
{
mouth_mc.mouth.gotoAndPlay(phrase)
last_frame=0 //reset last frame
loop_started=false
start_time = getTimer()
addEventListener(Event.ENTER_FRAME,lipSyncTracking)
}private function lipSyncTracking(e:Event = null):void
{
if (mouth_mc.mouth)
{
var cframe:int = mouth_mc.mouth.currentFrame //get the current frame
var ctime:int = getTimer() //current time
var time_expired:Number = (ctime – start_time )
var target_frame:int = Math.floor((FPS/1000)*time_expired)
if(target_frame>1 && cframe>1)loop_started=true //need to check for looping
if (cframe == last_frame || (cframe==1 && loop_started))
{//MC must have stopped or looped back to beginning
removeEventListener(Event.ENTER_FRAME,lipSyncTracking)
}
else
{//If frame is out by the margin of error then correct
if (target_frame >= cframe + ERROR_MARGIN)
{
mouth_mc.mouth.gotoAndPlay(target_frame)
}
}last_frame = cframe
}
}