从ios库的视频中获取持续时间和创建日期

时间:2013-01-22 03:56:00

标签: ios objective-c cocoa-touch video

我试图从照片库中选择/压缩视频,但是当我去获取持续时间和创建日期时,它们都返回null(持续时间默认为0.0秒)。我不确定我在这里做错了什么。

- (void)imagePickerController:(UIImagePickerController *)uploadPick didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

 if (CFStringCompare (( CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
 {
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    //Video Duration:
            MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
                                   initWithContentURL:videoURL];

    VideoTime.text = [NSString stringWithFormat:@"Time: %.2f", mp.duration];

    //Video Creation Date
    NSDictionary *metadataDictionary = (NSDictionary *)[info valueForKey:UIImagePickerControllerMediaMetadata];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *stringDate = [dateFormatter stringFromDate:metadataDictionary.fileCreationDate];
    [dateFormatter release];
    VideoDateTaken.text = [NSString stringWithFormat:@"Date Taken: %@", stringDate];

 }
}

1 个答案:

答案 0 :(得分:3)

根据documentationUIImagePickerControllerMediaMetadata仅对静止图像有效:

  

此密钥仅在使用源类型为的图像选择器时有效   设置为UIImagePickerControllerSourceTypeCamera,仅适用于   静止图像。

为了获得您想要的元数据,请使用ALAssetmetadata方法。