How to access videos from Photo Album and play them?

Go To


Following code used to save videos to photo Album.

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

        NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath]; 

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

Help Me.

2012-04-04 06:58
by Shreedhar
Try Updated code..! - Sarah 2012-04-04 08:03


with the code check if is image or video :

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

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

To Play the video check the 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
    [moviePlayer release];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

- (void) movieFinishedCallback:(NSNotification*) aNotification 
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter]
    [player autorelease];
2012-04-04 07:03
by Sarah
i Know how to check it.. i want if it is video how to play i - Shreedhar 2012-04-04 07:05
for that I have given the URL. Check it - Sarah 2012-04-04 07:18


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:@""])
    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).

2012-04-04 07:10
by tipycalFlow
Its not playing vide - Shreedhar 2012-04-04 07:46
@Shreedhar I've done this in the - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method - tipycalFlow 2012-04-04 08:20
Doesn't work.. - c1pherB1t 2014-07-15 15:54
@c0d3Junk13 Please be specific. What part doesn't work? Did you try debugging it - tipycalFlow 2014-07-16 05:11
In my scenario, I'm trying to play a recently captured and saved video (to photos album) using the camera (file name is or something like that) and even if the URL that is getting passed is correct, it is not playing the video... I'm debugging it myself right now. If I pass a local video (.mp4) that is in the project it works - c1pherB1t 2014-07-17 16:13
Ok, my problem was related to this: The player was not being retained... I'll take off the down mark on your answer if you can make an edit : - c1pherB1t 2014-07-17 16:18
Ah, I see! Apparently, I had fixed a similar problem a long time back myself simply by retaining the player! I've edited the answer to reflect the case with arc - tipycalFlow 2014-07-18 05:28
Thanks, at least its a more complete answer now, you got my up-vote for that : - c1pherB1t 2014-07-21 18:42