使用UIImagePickerController播放录制的视频

时间:2013-04-13 18:53:51

标签: iphone uiimagepickercontroller mpmovieplayercontroller

我正在创建一个使用UIImagePickerController录制视频的应用,我将录制的视频保存在文档目录中,然后点击USE尝试播放视频(想要显示完整视频)第一帧录制的视频以及中间的播放按钮,就像iPhone的原生应用程序一样。

我能够录制视频并将其保存到文档目录中,但无法创建帧和播放。

我尝试继续上课,我可以播放录制的视频,但点击USE按钮就会崩溃。

这是我的代码

-(void)btnRecord_Press
{
    BOOL canRecordVideo;

    canRecordVideo = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

    if (canRecordVideo)
    {
        UIImagePickerController *videoRecorder = [[UIImagePickerController alloc]init];
        videoRecorder.sourceType = UIImagePickerControllerSourceTypeCamera;
        videoRecorder.delegate=self;

        videoRecorder.showsCameraControls = TRUE;

        NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

        NSArray *videoMediaTypesOnly = [mediaTypes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF CONTAINS %@)",@"movie"]];

        BOOL movieOuputPossible  = (videoMediaTypesOnly!=nil);

        if (movieOuputPossible)
        {
            videoRecorder.mediaTypes = videoMediaTypesOnly;

            [self presentViewController:videoRecorder animated:YES completion:nil];
        }

    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];

    BOOL success = [videoData writeToFile:tempPath atomically:NO];


    [self dismissViewControllerAnimated:NO completion:nil];

    PlayMovie *play = [[PlayMovie alloc]initWithNibName:@"PlayMovie" bundle:Nil];

    [self.navigationController pushViewController:play animated:YES];

}

任何帮助都会很明显。

由于

1 个答案:

答案 0 :(得分:3)

我使用此代码完成了此操作。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    [self dismissViewControllerAnimated:NO completion:nil];

    [self Play];

}

-(void)Play
{

    NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];

    BOOL success = [videoData writeToFile:tempPath atomically:NO];

    player = [[MPMoviePlayerController alloc]initWithContentURL:videoURL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(btnDone_Press) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

    self._player.shouldAutoplay = NO;

    UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {        
        screenSize = [[UIScreen mainScreen]bounds].size;


        if (screenSize.height >480.0f)
        {
            player.view.frame = CGRectMake(0, 0, 320, 548);

        }
        else
        {
            player.view.frame = CGRectMake(0, 0, 320, 460);

        }

    }

    [self.view addSubview:player.view];

    self._player.scalingMode = MPMovieScalingModeAspectFit;

    self._player.fullscreen = YES;

    [self._player play];
}