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!
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) ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With