[英]application crash on select video file from uiimagepickercontroller
In my application i want to select the video from library by using UIImagePickerController. 在我的应用程序中,我想使用UIImagePickerController从库中选择视频。 but when i select the video which has more than duration of 5 minute, my application is crash on ipod. 但是,当我选择时长超过5分钟的视频时,我的应用程序在ipod上崩溃了。 how can i restrict the user to select the video which has duration more than 5 minute. 我如何限制用户选择时长超过5分钟的视频。 please give me solution of this bug. 请给我这个错误的解决方案。 Thank you. 谢谢。
Try this: 尝试这个:
NSURL * aUrlMovie = [[info valueForKey:UIImagePickerControllerMediaURL] retain];/ AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:aURLPath]; CMTime duration = playerItem.duration; float seconds = CMTimeGetSeconds(duration);
Then add validation for the number of seconds you wish.. 然后添加所需秒数的验证。
#pragma mark UIImagePickerController delegate
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ( [mediaType isEqualToString:(NSString*)kUTTypeMovie] ) {
self.mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum( mediaPath ) ) {
UISaveVideoAtPathToSavedPhotosAlbum( mediaPath,
self,
@selector(video:didFinishSavingWithError:contextInfo:),
NULL );
}
}
[self dismissModalViewControllerAnimated:YES];
}
Try this picker.videoMaximumDuration = 5; 试试这个picker.videoMaximumDuration = 5;
And something like this: 像这样:
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAsset.h>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);
//NSLog(@"duration: %.2f", seconds);
if(seconds > 300)
{
[picker popViewControllerAnimated:YES];
}
else{
[picker dismissModalViewControllerAnimated:YES];
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.