How to sync video and audio?

Background

I use AVAssetWriterInput.append to append sample buffer to the writer. Sometimes, I switch off the audio input(if user wants to temporarily disable audio input), so the append method will not be executed while the append method in video input will always be executed.

Problem

If user pause the audio and resume it later. The audio after resuming will immediately begin when user pause it (in the final video).

Example

'=' refers to CMSampleBuffer.

'|' means user paused the audio input.


Video: ---------------=================================

Audio(expected): ----=======|----------------=============

Audio(I got): ---------=======|=============----------------


I have printed the presentationTime from the audio sample buffer, it turns out it's correct.

Maybe my understanding to the AVAssetWriterInput.append is wrong?

My current solution is to always append the buffer, but when user wants to pause, I simply append an empty SampleBuffer filled with nothing.

I don't think this is the best way to deal with it.

Is there any idea to sync the buffer time with the video??

Have you tried using kCMSampleBufferAttachmentKey_EmptyMedia?

How to sync video and audio?
 
 
Q