iphone - 如何从相册中获取视频并播放

标签 iphone ios xcode

请帮我举一个从相册中获取视频并播放的示例。

以下代码不起作用

NSURL *URL = [info objectForKey:UIImagePickerControllerMediaURL];
  MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:aurl];
        [self.view addSubview:moviePlayerController.view];
        moviePlayerController.useApplicationAudioSession = NO;
        moviePlayerController.fullscreen = YES;
        [moviePlayerController play];

最佳答案

 // Choose video from library                   
                        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                        imagePicker.delegate = self;
                        imagePicker.mediaTypes =
                        [UIImagePickerController availableMediaTypesForSourceType:
                         imagePicker.sourceType];
                        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                        [self presentModalViewController:imagePicker animated:YES];
                        [imagePicker release];

//获取视频

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]){
        NSLog(@"found an image");
    }
    else if ([mediaType isEqualToString:@"public.movie"]){
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSLog(@"found a video");
        NSData *webData = [NSData dataWithContentsOfURL:videoURL];
       }

}

//播放视频

MPMoviePlayerViewController *movie = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self.view addSubview:movie.view];
[self presentMoviePlayerViewControllerAnimated:movie];
[movie release];

关于iphone - 如何从相册中获取视频并播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10023939/

相关文章:

ios - 是否允许在 iPhone 应用程序中加载您自己的广告?

iphone - 另一个应用程序可以注册我的自定义 url 方案吗

iOS5 : How to use a UIActionSheet object in an NSObject class and cause it to appear in another view?

iphone - 没有标题动画的pushViewController

ios - postNotification 上的 OneSignal 错误

ios4 - uiwebview 中的滚动条

iphone - UIBarButtonItem 图标通过 IB 添加时为白色,以编程方式添加时为黑色

c# - Monotouch类

ios - 嵌入式 Youtube 视频 - UIWebView 和 Safari 之间的区别

Xcode: Storyboard选项卡式应用程序来回传递数据