I have a need to detect when a specific time stamp is reached in an AKPlayer
. When that point is reached, I move the playhead back to repeat a section of the song to support a vamp feature in the app.
My current approach is to use a Timer
that's fired every millisecond to check if the player.currentTime
has met or exceeded the specific time. The problem I see is the currentTime
is not changing rapidly enough to detect this - it seems to update about every 11 milliseconds which isn't precise enough for my use case. The audio exceeds the time stamp before I catch it. Here you can see the value doesn't change each time the timer is fired, and when it does change it's a difference of 11.6 milliseconds, so if I needed to know when the player hit 0.5125 seconds, I wouldn't detect that until the player had run about 10 milliseconds over that time.
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5108390022675737
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
0.5224489795918368
Another option I explored was trying to observe the currentTime
with KVO but unfortunately this is never invoked.
currentTimeObserver = player.observe(\.currentTime) { (player, change) in
print("Changed to \(player.currentTime)")
}
Is there a way to more precisely track the currentTime
or otherwise register to be notified when it reaches a specific time stamp?