How to access videos from Photo Album and play the

2019-08-01 16:19发布

Following code used to save videos to photo Album.

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

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

If it is video i want to play it if it is image i want to display it.

Help Me.

2条回答
祖国的老花朵
2楼-- · 2019-08-01 16:53

In your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method, you can get the url of the video and play it:

@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];
}

Of course, you'll have to import the MediaPlayer.framework

EDIT: A majority of Cocoa projects now use arc, so the above code would require you retain the MPMoviePlayerController instance yourself (as mentioned in this answer).

查看更多
女痞
3楼-- · 2019-08-01 17:00

with the code check if is image or video :

    NSString *mediaType1 = [info objectForKey:UIImagePickerControllerMediaType];
    NSLog(@"mediaType : %@",mediaType1);

    if ([mediaType1 isEqualToString:@"public.image"])
    {
        //Show Image
}
else
{
    //show Video
}

To Play the video check the URL.

EDIT:

        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];
}
查看更多
登录 后发表回答