Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMoviePlayerController - Duration always 0

iPhone4, iOS 4.3.3, iOS SDK4.3

Hi all,

I'm creating a video upload feature. The videos are retrieved using UIImagePickerController and can be captured using the camera or picked from the photo library. I have an application constraint of 60 seconds max duration. This is easily achieved when recording the video using the camera via:

// Limit videos to 60 seconds

[picker setVideoMaximumDuration:60];

However when the video is selected from the photo library the only way I see of obtaining the duration is via MPMoviePlayerController duration property as follows:

// MediaType can be kUTTypeImage or kUTTypeMovie.

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

// if its a movie

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

    // get the URL
    NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
    NSLog(@"%@",mediaURL);

    // can use MPMoviePlayerController class to get duration
    int duration = -1;
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: mediaURL];
    if (moviePlayer != nil) {

        duration = moviePlayer.duration;
        NSString *path = moviePlayer.contentURL;

        [moviePlayer release];
    }

however the duration is always 0. I know the video has a duration because the duration is displayed as part of the subtitle when selecting it in the photo library. I understand that duration may not always be available but in this case the duration is displayed in photo lib. I also check the contentURL property and it has a good value. I'm able to retrieve the file, get its filesize etc so I know the NSURL of the file is good...

Thanks!

like image 914
imobilizer Avatar asked May 12 '11 18:05

imobilizer


1 Answers

I don't see anything immediately wrong with your code. However, using AVFoundation is much faster for this type of operation. Here's a code snippet to get the duration using AVFoundation:

AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:anURI 
                      options:[NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithBool:YES], 
                      AVURLAssetPreferPreciseDurationAndTimingKey,
                      nil]] autorelease];

NSTimeInterval durationInSeconds = 0.0;
if (asset) 
    durationInSeconds = CMTimeGetSeconds(asset.duration) ;
like image 92
memmons Avatar answered Oct 09 '22 10:10

memmons