2012-08-06 23 views
1

我正在从视频库中获取视频或录制新视频。一旦在imagepicker方法中选择了didfinishpickingmediawithinfo:我需要查找从中选择的视频持续时间我的视频专辑也是我从相机录制的。 我已经使用AVAsset获取CMTime的持续时间 我也提到MPMoviePlayer获取视频的持续时间,但他们不提供这样的属性。获取通过iPhone摄像头选择或录制的视频长度xcode

任何帮助,将不胜感激

问候

回答

0

的MPMoviePlayerController有一个属性的持续时间。

@property (nonatomic, readonly) NSTimeInterval duration 

请注意,这家酒店是只读

作为每文档

如果电影的持续时间是未知的,在该属性中的值是0.0。如果随后确定持续时间,则更新此属性并发布MPMovieDurationAvailableNotification通知。

但为什么你更喜欢MPMoviewPlayerController?这种类型的操作AVFoundation要快得多。而且您不需要等待MPMoviewPlayerController中的任何通知。

如果你想通过AVAsset。这里是一个确切的答案:MPMoviePlayerController - Duration always 0

1
NSURL *recordedTmpFile = [info objectForKey:UIImagePickerControllerMediaURL]; 

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:recordedTmpFile]; 

CMTime duration = playerItem.duration; 
float seconds = CMTimeGetSeconds(duration); 
1

1)只需添加AVFoundation framwork在您的应用程序

2)然后导入头文件到您的控制器

#import <AVFoundation/AVFoundation.h> 

3)然后加入以上代码由胜利者撰写。

NSURL *recordedTmpFile = [info objectForKey:UIImagePickerControllerMediaURL]; 

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:recordedTmpFile]; 

CMTime duration = playerItem.duration; 
float seconds = CMTimeGetSeconds(duration); 
0

其他的答案使用AVPlayerItem这个问题并没有为我工作,但这并使用AVURLAsset:

#import <AVFoundation/AVFoundation.h> 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSURL *videoURL=[info objectForKey:@"UIImagePickerControllerMediaURL"]; 
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 

    NSTimeInterval durationInSeconds = 0.0; 
    if (asset) 
     durationInSeconds = CMTimeGetSeconds(asset.duration); 
}