开发者

objectiveC/CMTime - convert AVPlayer.duration to milliseconds

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 her开发者_JAVA技巧e gets the value in seconds. How can I get the exact currentTime value but in milliseconds?


There is a way to get current times in seconds, you can take 1000 times that and ther you have your miliseconds:

Float64 dur = CMTimeGetSeconds([player currentTime]);
Float64 durInMiliSec = 1000*dur;

Sadly you will have to use it as a float or Float64 you can't use that result as a CMTime


I think that the accepted answer loses detail. A more accurate way to get milliseconds would be the following:

let seconds : Double = Float64(time.value * 1000) / Float64(time.timescale)

If you convert to seconds first you'd lose precision.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜