iPhone:如何从图书馆中选择视频的持续时间? [英] iPhone :How to get duration of video selected from library?
问题描述
我正在使用 UIImagePickerController
从库中选择视频文件。用户可以上传视频。
I am using UIImagePickerController
to choose video file from library. And user can upload the video.
我也在使用 videoMaximumDuration
属性,而用户想要捕获视频并上传。
Also I am using videoMaximumDuration
property while user wants to capture video and upload it.
我想知道如何获得所选视频文件的持续时间?这样我就可以限制用户上传持续时间超过20秒的视频。
I want to know that How can I get the duration of selected video file ? so that I can restrict the user to upload video that has duration more then 20 seconds.
我可以通过以下代码获得有关所选视频的一些基本信息:
I am able to get some basic info about selected video by this code :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
NSError *error;
NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:selectedVideoUrl.path error:&error];
NSNumber * size = [properties objectForKey: NSFileSize];
NSLog(@"Vide info :- %@",properties);
}
但没有关于所选视频的持续时间。
But there is nothing about duration of selected video.
谢谢...
推荐答案
得到解决方案:我用 AVPlayerItem
class和 AVFoundation
和 CoreMedia
框架。
Got the solution : I use AVPlayerItem
class and AVFoundation
and CoreMedia
framework.
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAsset.h>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);
NSLog(@"duration: %.2f", seconds);
}
这篇关于iPhone:如何从图书馆中选择视频的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!