2015-03-25 67 views
4

我知道視頻類型問題的持續時間已被回答,但我面臨使用AVAssetAVURLAsset獲取.mp4文件的持續時間的實際麻煩。我使用下面的代碼iOS-如何使用AVAsset或AVURLAsset獲得.mp4文件的持續時間

NSString *itemPathString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:obmodel.actualname]; 
NSURL *itemPathURL = [NSURL URLWithString:itemPathString]; 

if([[NSFileManager defaultManager] fileExistsAtPath:itemPathString]){ 

    NSLog(@"File Exists"); 
} 

AVAsset *videoAsset = (AVAsset *)[AVAsset assetWithURL:itemPathURL]; 
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 

NSLog(@"duration: %.2f", CMTimeGetSeconds(videoAssetTrack.timeRange.duration)); 
if(CMTimeGetSeconds(videoAsset.duration) >= 59.0){ 
} 

但每次返回0 我還引進時間如下headers

#import <AVFoundation/AVAsset.h> 
#import <CoreMedia/CoreMedia.h> 
#import <AVFoundation/AVFoundation.h> 

我也嘗試了一些其他的代碼,我知道,但我沒有一個工作。 我的視頻工作正常,如果我通過iExplorer播放它,我的路徑很好,但不知道我錯過了什麼。 我想分享一下,我發現的是我的AVAssets沒有正確加載 請建議我應該做些什麼。

+0

[This answer](http://stackoverflow.com/a/19327187/1702077)可能會有所幫助。 – 2015-03-25 12:36:24

+0

@ChrisLoonam感謝您的回覆。我用他的方法,它返回了我'AVKeyValueStatusFailed' 在我的情況下會是什麼錯誤? – 2015-03-25 12:46:07

+0

trackKey應該是什麼? – 2015-03-25 12:47:53

回答

7

是的,我以前有類似的問題。對我來說,問題就走了,當我用:

NSURL *itemPathURL = [NSURL fileURLWithPath:itemPathString]; 

,而不是

NSURL *itemPathURL = [NSURL URLWithString:itemPathString]; 

如果你完全確定你的路徑是確定的,這應該解決您的問題。

希望這會有所幫助!

相關問題