2015-07-01 115 views
4

我需要使用swift創建自定義視頻插件。但我不知道如何獲得視頻全部持續時間和當前播放時間。在我的控制檯剛出現這個輸出,C.CMTime。我不確定我的代碼有什麼問題。如何獲取視頻全部持續時間和當前播放時間?

我的代碼

let url = NSBundle.mainBundle().URLForResource("Video", withExtension:"mp4") 
let asset = AVURLAsset(URL:url, options:nil) 
let duration: CMTime = asset.duration 

println(duration) 

請指點。謝謝。

回答

5

您可以使用CMTimeGetSeconds將CMTime轉換爲秒。

let durationTime = CMTimeGetSeconds(duration) 
+0

進口AVFoundation – NSDeveloper

+0

我得到這個錯誤信息, 'Float64' 是無法轉換爲 'CMTime' –

+0

爲什麼你再次轉換Float64到CMTime? – NSDeveloper

0

使用IOS目的C概念

- (NSTimeInterval) playableDuration 
{ 
    // use loadedTimeRanges to compute playableDuration. 
    AVPlayerItem * item = _moviePlayer.currentItem; 

    if (item.status == AVPlayerItemStatusReadyToPlay) { 
    NSArray * timeRangeArray = item.loadedTimeRanges; 

    CMTimeRange aTimeRange = [[timeRangeArray objectAtIndex:0] CMTimeRangeValue]; 

    double startTime = CMTimeGetSeconds(aTimeRange.start); 
    double loadedDuration = CMTimeGetSeconds(aTimeRange.duration); 

    // FIXME: shoule we sum up all sections to have a total playable duration, 
    // or we just use first section as whole? 

    NSLog(@"get time range, its start is %f seconds, its duration is %f seconds.", startTime, loadedDuration); 


    return (NSTimeInterval)(startTime + loadedDuration); 
    } 
    else 
    { 
    return(CMTimeGetSeconds(kCMTimeInvalid)); 
    } 

} 
+0

對不起,我沒有迅速的語言想法 –

+0

,但在運作良好的objective-c –

相關問題