Search code examples
ioscocoa-touchuiimagepickercontrollermpmovieplayercontroller

Find mediaPath after UIImagePicker and UISaveVideoAtPathToSavedPhotosAlbum


I Have an image picker where I pick a movie (which can be edited). I then grab a thumbnail using;

UIImage *tempImage = [moviePlayerThumb thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

This work fine and I can display the image in another method no problems. I also store the video data to upload later;

[mediaDict setValue:[NSData dataWithContentsOfFile:mediaPath] forKey:@"data"];

This also works as once uploaded the file plays no problem from the server.

The last things I store are the URL and Path, however, when I come to put these in a player later I get stuck with the player saying "Loading" (plus spinny thing);

I think it is because the picker after editing stores the video in a temp directory which (when I come to play the file later) has been emptied.

The paths are like this;

file://localhost/private/var/mobile/Applications/B26A085A-C471-4972-9070-5A3F8DC48449/tmp//trim.ttCbAg.MOV

I tried to make the picker store the videos permanently like this;

    NSString *mediaPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

    NSURL *mediaURL=[info objectForKey:UIImagePickerControllerMediaURL];
    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (mediaPath))
    {
        UISaveVideoAtPathToSavedPhotosAlbum (mediaPath, nil, nil, nil);
    }

I know this is working because my Photo Album is slowly filling up with edited versions of my video. The problem is how do I find out the path of the file AFTER the UISaveVideo method?

The player logs this;

[MPAVController] Autoplay: Disabling autoplay for pause
[MPAVController] Autoplay: Disabling autoplay
[MPAVController] Autoplay: Disabling autoplay for pause
[MPAVController] Autoplay: Disabling autoplay
[MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
[MPAVController] Autoplay: Enabling autoplay

The player code looks like this (I have tried using the mediaPath and mediaURL);

    // Play the movie
    NSURL *tempMediaPath = [NSURL fileURLWithPath:images[indexPath.item][@"path"]];

    NSURL *tempMediaURL = images[indexPath.item][@"URL"];

    NSLog(@"Media Path, URL : %@, %@", tempMediaPath, tempMediaURL);

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:tempMediaPath];

    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];
    [moviePlayer prepareToPlay];
    [moviePlayer play];

And the NSLog;

Media Path, URL : file://localhost/private/var/mobile/Applications/B26A085A-C471-4972-9070-5A3F8DC48449/tmp//trim.ttCbAg.MOV, file://localhost/private/var/mobile/Applications/B26A085A-C471-4972-9070-5A3F8DC48449/tmp//trim.ttCbAg.MOV

Any help really appreciated.


Solution

  • I did the same in another project and the following code works in both embedded mode as well as fullscreen:

    _movieController = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
    _movieController.view.frame = self.view.bounds;
    _movieController.controlStyle = MPMovieControlStyleEmbedded;
    _movieController.backgroundView.hidden = YES;
    _movieController.contentURL = self.movieURL;
    [_movieController prepareToPlay];
    _movieController.scalingMode = MPMovieScalingModeAspectFit;
    _movieController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _movieController.shouldAutoplay=YES;
    _movieController.movieSourceType = MPMovieSourceTypeFile;
    [self.view addSubview:_movieController.view];