xcode 如何从iphone库导入视频并在应用程序中播放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6266681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:12:47  来源:igfitidea点击:

how to import Video from iphone library and play in application

xcodeipaduiimagepickercontroller

提问by vensan

i am using following code to show video library

我正在使用以下代码来显示视频库

 -(IBAction)showVideoLibrary
{
UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
videoPicker.delegate = self;
videoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

videoPicker.mediaTypes =[[NSArray alloc] initWithObjects: (NSString *)kUTTypeMovie,nil];    
if(self.popoverController!=nil)
{
    [self.popoverController release];
}
self.popoverController  = [[UIPopoverController alloc] initWithContentViewController:videoPicker];
popoverController.delegate = self;
popoverController.popoverContentSize=CGSizeMake(320,1000);

[popoverController presentPopoverFromRect:CGRectMake(0,0,10,10) inView:self.view permittedArrowDirections:nil animated:YES];

}

and for receiving the selected Video Url i am using following function

并为了接收选定的视频网址,我使用以下功能

- (void)imagePickerController: (UIImagePickerController *)picker2 didFinishPickingMediaWithInfo: (NSDictionary *)info {

NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:@"public.movie"])
{
    NSLog(@"came to video select...");

     NSURL *videoUrl=(NSURL*)[info objectForKey:@"UIImagePickerControllerMediaURL"];

    NSLog(@"Got Movie Url==%@",videoUrl);


}

this is the code i am using, i can see list of video's present in library, but when i press "USE" button, it is showing Compressing video.. and i am unable to cancel it, and i am not getting any kind of url in the

这是我正在使用的代码,我可以看到库中存在的视频列表,但是当我按下“使用”按钮时,它显示正在压缩视频..我无法取消它,而且我没有得到任何类型的网址在

- (void)imagePickerController: (UIImagePickerController *)picker2 didFinishPickingMediaWithInfo: (NSDictionary *)info

function.. Any ideas to solve this problem.. and get url in to the app.

功能.. 解决这个问题的任何想法.. 并在应用程序中获取 url。

Thanks.

谢谢。

回答by Dinesh Kaushik

Try it on Real iPhone Device

在真正的 iPhone 设备上试用

here is the code for picking video from iPhone library which i have used in my project

这是我在我的项目中使用的从 iPhone 库中挑选视频的代码

Just add video method from selector to your desired button

只需将选择器中的视频方法添加到您想要的按钮

  -(void)video
     {
     UIImagePickerController *imagePicker =
     [[UIImagePickerController alloc] init];
     imagePicker.delegate = self;
     imagePicker.sourceType = 
     UIImagePickerControllerSourceTypePhotoLibrary;


     imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];

     [self presentModalViewController:imagePicker animated:YES];


     }


     -(void) imagePickerController: (UIImagePickerController *) picker
     didFinishPickingMediaWithInfo: (NSDictionary *) info 
     {


     NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

     if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
     == kCFCompareEqualTo) 
     {

     NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

       NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
     // NSLog(@"%@",moviePath);

     if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
     UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
     }
     }


     [self dismissModalViewControllerAnimated:YES];

     [picker release];


     }

Do not forget to add mobile core services framework

不要忘记添加移动核心服务框架

and to import

并导入

 #import <MobileCoreServices/UTCoreTypes.h>

the string "moviepath" give you the path of the video in that iPhone then perform any desired thing with that video. You will get video path after compressing is done in string movie path and "videourl" is url for that video To play that video

字符串“moviepath”为您提供该 iPhone 中视频的路径,然后对该视频执行任何所需的操作。在字符串电影路径中完成压缩后,您将获得视频路径,“videourl”是该视频的 url 播放该视频

 MPMoviePlayerController *player =[[MPMoviePlayerController alloc] initWithContentURL: url];   //  give here the "videourl"  
    [[player view] setFrame: [self.view bounds]];  
    [self.view addSubview: [player view]];
    [player play];