2 Replies
      Latest reply on Sep 11, 2019 12:51 PM by hotpaw2
      jcurtis Level 1 Level 1 (0 points)

        Hi everyone,


        I'm seeking to create a precision timer that synchronises with AVPlayer at a rate of 10 milliseconds.


        Every 10ms I want to trigger an event. My current implementation uses an NSTimer which causes clock drift.


        From what I've found it seems that the best implementation would be to use the audio rate clock - but I'm struggling to find a clear answer how to implement this.


        Is this possible using AVFoundation instead of digging down deeper into Core Audio?

        • Re: Precision timer synchronisation with AVPlayer (10ms)
          jcurtis Level 1 Level 1 (0 points)

          I attemped using the addPeriodicTimeObserver method to AVPlayer instead of an NSTimer and still finding clock drift.


          From what I can gather, it's looking like CMAudioClock is the way to go here. But I'm finding the developer page a little unclear.


          Can anyone here point me toward a good resource for understanding Core Media integration with AV Foundation (that covers sync)?

          • Re: Precision timer synchronisation with AVPlayer (10ms)
            hotpaw2 Level 2 Level 2 (95 points)

            In what thread are you requesting timer callbacks?  What do you mean by "clock drift"?


            In general, in the main UI runloop thread, timer callback resolution is usually no better than roughly the display frame rate, 60 Hz, or every 16.667 mS.  For better 60 Hz timing, try the CADisplayLink API.  For higher timer resolution, you might want to use a gcd timer instead of an NSTimer, dispatch your timer callbacks to a dedicated thread or operation queue, perhaps request each next callback for a slightly shorter time interval than the desired interval, followed by a polling spin-wait until the media time increments to the needed time value for your trigger.