ObjectiveC/CMTime - 将 AVPlayer.duration 转换为毫秒
我正在创建一个用于播放铃声的应用程序,我想知道每次播放铃声的当前时间(以毫秒为单位)。
CMTime cTime =player_.currentTime; 浮动当前时间 = cTime.value / cTime.timescale;
这里的 currentTime 获取以秒为单位的值。 如何获得精确的 currentTime 值(以毫秒为单位)?
I'm creating an app for playing a ringtone and I'd want to know the current time in milliseconds of the played ringtone every time.
CMTime cTime = player_.currentTime;
float currentTime = cTime.value / cTime.timescale;
That currentTime here gets the value in seconds.
How can I get the exact currentTime value but in milliseconds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一种方法可以以秒为单位获取当前时间,您可以将其乘以 1000 倍,然后您就得到了毫秒:
遗憾的是您必须将其用作
float
或Float64
您不能将该结果用作CMTime
There is a way to get current times in seconds, you can take 1000 times that and ther you have your miliseconds:
Sadly you will have to use it as a
float
orFloat64
you can't use that result as aCMTime
我认为接受的答案丢失了细节。获取毫秒的更准确方法如下:
如果先转换为秒,则会失去精度。
I think that the accepted answer loses detail. A more accurate way to get milliseconds would be the following:
If you convert to seconds first you'd lose precision.
CMTime.value 和 CMTime.timescale 都是整数,因此根据您的代码判断,结果会四舍五入,并且您不会获得精确的时间戳。尝试使用 CMTimeGetSeconds 代替。
CMTime.value and CMTime.timescale are both integers, so judging by your code, the result gets rounded and you don't get a precise timestamp. Try CMTimeGetSeconds instead.