问题
I am trying to synchronize several CABasicAnimations
with AVAudioPlayer
. The issue I have is that CABasicAnimation
uses CACurrentMediaTime()
as a reference point when scheduling animations while AVAudioPlayer
uses deviceCurrentTime
. Also for the animations, CFTimeInterval
is used, while for sound it's NSTimeInterval
(not sure if they're "toll free bridged" like other CF and NS types). I'm finding that the reference points are different as well.
Is there a way to ensure that the sounds and animations use the same reference point?
回答1:
I don't know the "official" answer, but they are both double precision floating point numbers that measure a number of seconds from some reference time.
From the docs, it sounds like deviceCurrentTime is linked to the current audio session:
The time value, in seconds, of the audio output device. (read-only)
@property(readonly) NSTimeInterval deviceCurrentTime Discussion The value of this property increases monotonically while an audio player is playing or paused.
If more than one audio player is connected to the audio output device, device time continues incrementing as long as at least one of the players is playing or paused.
If the audio output device has no connected audio players that are either playing or paused, device time reverts to 0.
You should be able to start an audio output session, call CACurrentMediaTime() then get the deviceCurrentTime of your audio session in 2 sequential statements, then calculate an offset constant to convert between them. That offset would be accurate within a few nanoseconds. The offset would only be valid while the audio output session is active. You'd have to recalculate it each time you remove all audio players from the audio session.
回答2:
I think the official answer just changed, though currently under NDA.
See "What's New in Camera Capture", in particular the last few slides about the CMSync* functions. https://developer.apple.com/videos/wwdc/2012/?id=520
来源:https://stackoverflow.com/questions/11198893/reconciling-cacurrentmediatime-and-devicecurrenttime