2014-01-19 96 views
0

我有一个视频保存在应用程序文件目录。我需要做的是找出视频的持续时间。即它有多少分钟和几秒钟。我无法弄清楚的是,一旦我有了电影网址,我需要做些什么来获得它的持续时间?我是否使用ALAssets?但我不知道如何。视频/电影持续时间

这是我的代码到目前为止

NSString *nameSelected = [NSString stringWithFormat:@"Movie-1.mov"]; 
    NSLog(@"nameSelected: %@ ...", nameSelected); 



    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES); 
    NSString* documentsPath = [paths objectAtIndex:0]; 
    NSString* movieFile1 = [documentsPath stringByAppendingPathComponent:nameSelected]; 

    NSURL *firstMovieURL1 = [NSURL fileURLWithPath:movieFile1]; 

//No sure how I can call this 
    //NSString *durationStr = [alAsset valueForProperty:ALAssetPropertyDuration]; 
+0

找到我的答案http://stackoverflow.com/questions/8699105/iphone-how-to-get-duration-of-video-selected-from-图书馆/ 8699866#8699866 – Maulik

回答

3

这对我有效。

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:firstMovieURL1 
               options:[NSDictionary dictionaryWithObjectsAndKeys: 
                  [NSNumber numberWithBool:YES], 
                  AVURLAssetPreferPreciseDurationAndTimingKey, 
                  nil]]; 

    NSTimeInterval durationInSeconds = 0.0; 
    if (asset) 
     durationInSeconds = CMTimeGetSeconds(asset.duration) ; 
    NSLog(@"videoDuration: %.2f", durationInSeconds); 

确保导入

#import <AssetsLibrary/AssetsLibrary.h> 
+0

只需将'#import '添加到我的现有代码中即可。谢谢! –

0

利用AVPlayerItemAVFoundation & CoreMedia框架。

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:yourVideoUrl]; 
CMTime videoDuration = playerItem.duration; 
float seconds = CMTimeGetSeconds(videoDuration); 
NSLog(@"videoDuration: %.2f", seconds); 
+0

这没有奏效。我将此作为输出 - videoDuration:nan –