MPMoviePlayerController - Duration always 0

蓝咒 提交于 2019-12-08 16:57:09

问题


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!


回答1:


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) ;


来源:https://stackoverflow.com/questions/5982893/mpmovieplayercontroller-duration-always-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!