2016-07-18 102 views
-1

我以秒爲單位獲得當前播放值,但我需要以毫秒爲單位。我試圖currentTime.value/currentTime.scale。但它沒有得到確切的價值。如何獲取當前播放時間,AVPlayer中的CMTime以毫秒爲單位?

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]; 
+0

不了你做第二/ 1000? –

+0

@ HariKrishnan.P你確定你用1000乘以秒?大聲笑我很困惑! IT應該是秒/ 1000 –

+0

毫秒=秒/ 1000。它的權利之一。我錯誤地寫了 –

回答

2

好吧,首先,你想要的值millisecond不被秒

所以,你可以只使用CMTimeGetSeconds(< #CMTime時間#>)得到秒
然後,如果您想要毫秒,請使用秒/ 1000.f作爲float或double值

用於CMTime計算使用CMTime方法
CMTimeMultiplyByRatio(< #CMTime tim E·>,<#int32_t乘數#>,<#int32_t除數#>)
只是這樣做 - > CMTimeMultiplyByRatio(yourCMTimeValue,1,1000)

蘋果的文檔

@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 
+0

是的,我在閱讀了CMTime後得到了它。感謝您的回答:) –

+1

數學定律說毫秒是*乘以*秒乘以1000檢索 – codelearner

+0

@codelearner emmm?如果如你所說,1毫秒= 1秒乘以1000,1000毫秒如何等於1秒? –

相關問題