swiftの動画再生での話。
AVPlayer().seek(to time: CMTime)
でシークした後に動画再生を行うと、指定した時刻よりも最大0.5秒程度ずれた位置から再生が始まってしまい、なぜかと悩んでいた。
ドキュメント
https://developer.apple.com/documentation/avfoundation/avplayer/1385953-seek)
を見たらすぐに解決しました。
Discussion
The time to which the player is seeked may differ from the specified time for efficiency. For sample accurate seeking see seek(to:toleranceBefore:toleranceAfter:).
なるほど、正確な時刻にシークするためには、
AVPlayer().seek(to time: CMTime,
toleranceBefore: CMTime,
toleranceAfter: CMTime)
を使って、前後の誤差を小さく設定する必要があるとな。
ドキュメント読むのは大事ですね。
つまりデフォルトでは、-1〜1秒程度の誤差が許される設定になっているらしい。
おしまい。