10

I am interested in recording media using an AVCaptureSession in iOS while playing media back using an AVPlayer (specifically, I am playing back audio and recording video, but I'm not sure it matters).

The problem is, when I play the resulting media back together later, they are out of sync. Is it possible to synchronize them, either by ensuring that playback and recording start simultaneously, or by discovering what the offset is between them? I probably need the sync to be on the order of 10 ms. It is unreasonable to assume that I can always capture audio (since the user may use headphones), so syncing via analysis of original and recorded audio is not an option.

This question suggests that it's possible to end playback and recording simultaneously and determine the initial offset from the resulting lengths that way, but I'm unclear how to get them to end simultaneously. I have two cases: 1) the audio playback runs out, and 2), the user hits the "stop recording" button.

This question suggests priming and then applying a fixed, but possibly device-dependent delay, which is obviously a hack, but if it's good enough for audio it's obviously worth considering for video.

Is there another media layer I can use to perform the required synchronization?

Related: this question is unanswered.

Community
  • 1
  • 1
Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
  • This may help... http://stackoverflow.com/a/7219499/2700842 There's going to be a small delay. You could try to edit them back together. https://developer.apple.com/library/Mac/DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/03_Editing.html#//apple_ref/doc/uid/TP40010188-CH8-SW11 Just adjust the TimeRange on the leading asset to start a bit later. – Nick Terry Dec 10 '13 at 04:34
  • Did you ever get this to work without a delay? I'm having the same issue. I've tried using `setRate:time:atHostTime:` using the first samplebuffer's PTS, but it's still out of sync. – Sti Oct 20 '14 at 14:12

1 Answers1

0

If you are specifically using AVPlayer to playback Audio and i would suggest you to use AudioQueueServices for the same. Its seamless and fast as it reads buffer by buffer and play pause is faster than AVPLlayer

There can also be the possibility that you are missing the initial statement of [avPlayer prepareToPlay] which might be causing much overhead for it to sync before playing the Audio. Hope it helps you.