【问题标题】:get the length of video selected or Recorded through iPhone Camera xcode获取通过 iPhone 相机 xcode 选择或录制的视频长度
【发布时间】:2012-08-06 10:32:45
【问题描述】:
我正在从视频库中获取视频或录制新视频。一旦在 imagepicker 方法中选择了 didfinishpickingmediawithinfo:我需要找到从我的视频专辑中选择的视频的持续时间,以及我从相机录制的视频。
我使用 AVAsset 来获取 CMTime 中的持续时间
我还提到了 MPMoviePlayer 来获取视频的持续时间,但他们没有为我提供这样的属性。
任何帮助将不胜感激
问候
【问题讨论】:
标签:
iphone
video
xcode4
uiimagepickercontroller
duration
【解决方案1】:
NSURL *recordedTmpFile = [info objectForKey:UIImagePickerControllerMediaURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:recordedTmpFile];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);
【解决方案2】:
1) 只需在您的应用中添加 AVFoundation 框架
2) 然后将头文件导入你的控制器
#import <AVFoundation/AVFoundation.h>
3) 然后添加上面victor写的代码。
NSURL *recordedTmpFile = [info objectForKey:UIImagePickerControllerMediaURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:recordedTmpFile];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);
【解决方案3】:
MPMoviePlayerController 有一个属性 duration。
@property (nonatomic, readonly) NSTimeInterval duration
请注意,此属性是只读的。
根据文档
如果电影的持续时间未知,则此属性中的值为 0.0。如果随后确定了持续时间,则更新此属性并发布 MPMovieDurationAvailableNotification 通知。
但是为什么你更喜欢 MPMoviewPlayerController? AVFoundation 对于这种类型的操作要快得多。而且您不需要像在 MPMoviewPlayerController 中那样等待任何通知。
如果您想通过 AVAsset。这是一个确切的答案:MPMoviePlayerController - Duration always 0
【解决方案4】:
使用 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);
}