如何从UIImagePickerController中选择一个视频并进行播放?

时间:2012-07-20 04:08:14

标签: video uiimagepickercontroller mpmovieplayercontroller

目前我使用以下代码获取NSURL。但是在我拿到NSURL之后我该怎么玩呢?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
        {
            NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
            if ([mediaType isEqualToString:@"public.image"]){
                // UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
                UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
                NSLog(@"found an image");
            }
            else if ([mediaType isEqualToString:@"public.movie"]){
                NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
                NSLog([videoURL absoluteString]);
                NSLog(@"found a video");    
            }
            [picker dismissModalViewControllerAnimated:YES];
        }

2 个答案:

答案 0 :(得分:4)

- (void)selectVideoFromLibrary
{
   UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeMovie,
                                  nil];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     NSURL *movieURL = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
     MPMoviePlayerController *movieplayer =[[MPMoviePlayerController alloc] initWithContentURL: url];    
    [[movieplayer view] setFrame: [self.view bounds]];  
    [self.view addSubview: [movieplayer view]];
    [movieplayer play];
}

答案 1 :(得分:2)

url是您的视频网址

MPMoviePlayerController *plyr =[[MPMoviePlayerController alloc] initWithContentURL: url];    
[[plyr view] setFrame: [self.view bounds]];  
[self.view addSubview: [plyr view]];
[plyr play];