协调CACurrentMediaTime()和deviceCurrentTime [英] Reconciling CACurrentMediaTime() and deviceCurrentTime

查看:297
本文介绍了协调CACurrentMediaTime()和deviceCurrentTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AVAudioPlayer 同步几个 CABasicAnimations 。我遇到的问题是 CABasicAnimation 在调度动画时使用 CACurrentMediaTime()作为参考点,而 AVAudioPlayer 使用 deviceCurrentTime 。同样对于动画,使用 CFTimeInterval ,而对于声音,它是 NSTimeInterval (不确定它们是否是免费的桥接像其他CF和NS类型一样)。我发现参考点也不同。

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?

推荐答案

我不知道官方答案,但它们都是双精度浮点数,可以测量一些参考时间的秒数。

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.

从文档来看,听起来像deviceCurrentTime链接到当前的音频会话:

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讨论当播放音频播放器
时,此属性的
值单调增加或暂停。

@property(readonly) NSTimeInterval deviceCurrentTime Discussion The value of this property increases monotonically while an audio player is playing or paused.

如果音频输出设备连接了多个音频播放器,只要$ b $中的至少一个,
设备时间就会继续递增b播放器正在播放或暂停。

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.

如果音频输出设备没有播放或暂停
的连接音频播放器,设备时间将恢复为0。 / p>

If the audio output device has no connected audio players that are either playing or paused, device time reverts to 0.

您应该能够启动音频输出会话,调用CACurrentMediaTime()然后在2个连续语句中获取音频会话的deviceCurrentTime,然后计算偏移常数以在它们之间进行转换。该偏移在几纳秒内是准确的。
偏移量仅在音频输出会话处于活动状态时有效。每次从音频会话中删除所有音频播放器时,您都必须重新计算它。

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.

这篇关于协调CACurrentMediaTime()和deviceCurrentTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆