【问题标题】:How to access videos from Photo Album and play them?如何访问相册中的视频并播放它们?
【发布时间】:2012-04-04 06:58:44
【问题描述】:

以下代码用于将视频保存到相册。

else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {

        NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath]; 
        UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
    }

如果是视频我想播放它如果是图像我想显示它。

帮帮我。

【问题讨论】:

    标签: ios xcode ipad uiimagepickercontroller


    【解决方案1】:

    使用代码检查是图像还是视频:

        NSString *mediaType1 = [info objectForKey:UIImagePickerControllerMediaType];
        NSLog(@"mediaType : %@",mediaType1);
    
        if ([mediaType1 isEqualToString:@"public.image"])
        {
            //Show Image
    }
    else
    {
        //show Video
    }
    

    要播放视频,请查看URL

    编辑:

            NSString *moviePath = [bundle pathForResource:@"IMG_0017" ofType:@"MOV"];
            NSLog(@"moviePath : %@",moviePath);
         //   NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    
            NSURL *movieURL = [NSURL URLWithString:strValURL];
            NSLog(@"movieURL : %@",movieURL);
            if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2)
            {
                NSLog(@"> 3.2");
                MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
                if (mp)
                {
                    [self presentMoviePlayerViewControllerAnimated:mp];
                    mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
                    [mp.moviePlayer play];
                    [mp release];
                }
            }
            else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2)
            {
                NSLog(@"< 3.2");
                theMovie = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
                theMovie.scalingMode = MPMovieScalingModeAspectFill;
                [[NSNotificationCenter defaultCenter]
                 addObserver: self
                 selector: @selector(myMovieFinishedCallback:)
                 name: MPMoviePlayerPlaybackDidFinishNotification
                 object: theMovie];
                [theMovie play];
            }
    
    - (void) moviePlayBackDidFinish:(NSNotification*)notification
    {
        MPMoviePlayerViewController *moviePlayer = [notification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];
        [moviePlayer release];
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
    
    - (void) movieFinishedCallback:(NSNotification*) aNotification 
    {
        MPMoviePlayerController *player = [aNotification object];
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:MPMoviePlayerPlaybackDidFinishNotification
         object:player];
        [player autorelease];
    }
    

    【讨论】:

    • 我知道如何检查它.. 我想要如果它是视频如何播放它
    • 为此我已经给出了 URL。检查一下。
    【解决方案2】:

    在你的- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法中,你可以获取视频的url并播放:

    @property (nonatomic,retain) MPMoviePlayerController *moviePlayerController;
    
    if ([mediaType isEqualToString:@"public.movie"])
    {
        NSURL *aURL    = [info objectForKey:UIImagePickerControllerMediaURL];//get the url
        // and init the video player using this url
        _moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:aURL];
        [self.view _moviePlayerController.view];
        _moviePlayerController.useApplicationAudioSession = NO;
        _moviePlayerController.fullscreen = YES;
        [_moviePlayerController play];
    }
    

    当然,您必须导入MediaPlayer.framework

    编辑:现在大多数 Cocoa 项目都使用arc,因此上面的代码需要您自己保留MPMoviePlayerController 实例(如this answer 中所述)。

    【讨论】:

    • @Shreedhar 我已经在- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 方法中完成了这项工作。
    • @c0d3Junk13 请具体说明。哪一部分不行?你试过调试吗?
    • 在我的场景中,我正在尝试使用相机(文件名为 capture.mov 或类似名称)播放最近捕获并保存的视频(到相册),即使 URL正在通过是正确的,它没有播放视频......我现在正在自己调试它。如果我传递项目中的本地视频(.mp4),它就可以工作。
    • 好的,我的问题与此有关:stackoverflow.com/questions/15058138/… 玩家没有被保留...如果您可以进行编辑,我会在您的答案上取消标记:(跨度>
    • 啊,我明白了!显然,我很久以前就通过保留播放器解决了类似的问题!我已经编辑了答案以反映arc 的情况。
    猜你喜欢
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多