如何在AVPlayer中获得当前播放时间CMTime(以毫秒为单位)?

时间:2016-07-18 13:25:44

标签: ios objective-c avplayer

我在几秒钟内获得当前的游戏价值,但我需要几毫秒。我试过currentTime.value / currentTime.scale。但它没有得到确切的价值。

CMTime currentTime = vPlayer.currentItem.currentTime; //playing time
CMTimeValue tValue=currentTime.value;
CMTimeScale tScale=currentTime.timescale;

NSTimeInterval time = CMTimeGetSeconds(currentTime);
NSLog(@"Time :%f",time);//This is in seconds, it misses decimal value double shot=(float)tValue/(float)tScale;
shotTimeVideo=[NSString stringWithFormat:@"%.2f",(float)tValue/(float)tScale];

CMTime currentTime = vPlayer.currentItem.currentTime; //playing time
CMTimeValue tValue=currentTime.value;
CMTimeScale tScale=currentTime.timescale;

NSTimeInterval time = CMTimeGetSeconds(currentTime);
NSLog(@"Time :%f",time);//This is in seconds, it misses decimal value   
double shot=(float)tValue/(float)tScale;
shotTimeVideo=[NSString stringWithFormat:@"%.2f", (float)tValue/(float)tScale];

2 个答案:

答案 0 :(得分:2)

好的,首先,你想要的值是millisecond而不是秒

所以,您可以使用 CMTimeGetSeconds(< #CMTime time#>)来获取秒数然后,如果您想要毫秒,则使用秒/ 1000.f进行浮点运算或者双值

用于CMTime计算使用CMTime方法
CMTimeMultiplyByRatio(< #CMTime time#>,< #int32_t multiplier#>,< #int32_t divisor#>)左右这样做 - - > CMTimeMultiplyByRatio(yourCMTimeValue,1,1000)

Apple的文档

@function   CMTimeMultiplyByRatio
@abstract   Returns the result of multiplying a CMTime by an integer, then dividing by another integer.
@discussion The exact rational value will be preserved, if possible without overflow.  If an overflow
            would occur, a new timescale will be chosen so as to minimize the rounding error.
            Default rounding will be applied when converting the result to this timescale.  If the
            result value still overflows when timescale == 1, then the result will be either positive
            or negative infinity, depending on the direction of the overflow.

            If any rounding occurs for any reason, the result's kCMTimeFlags_HasBeenRounded flag will be
            set.  This flag will also be set if the CMTime operand has kCMTimeFlags_HasBeenRounded set.

            If the denominator, and either the time or the numerator, are zero, the result will be
            kCMTimeInvalid.  If only the denominator is zero, the result will be either kCMTimePositiveInfinity
            or kCMTimeNegativeInfinity, depending on the signs of the other arguments.

            If time is invalid, the result will be invalid. If time is infinite, the result will be
            similarly infinite. If time is indefinite, the result will be indefinite.                               


@result     (time * multiplier) / divisor

答案 1 :(得分:0)

有点晚,但可能对其他人有用。 您可以通过

从CMTime对象获取以毫秒为单位的时间戳
CMTimeConvertScale(yourCMTime, timescale:1000, method:
    CMTimeRoundingMethod.roundHalfAwayFromZero

一个例子:

var yourtime:CMTime = CMTimeMake(value:1234567, timescale: 10)
var timemilli:CMTime = CMTimeConvertScale(yourtime, timescale:1000, method:
    CMTimeRoundingMethod.roundHalfAwayFromZero)
var timemillival:Int64 = timemilli.value
print("timemilli \(timemillival)")

将产生

timemilli 123456700