Search code examples
iphonempmovieplayercontroller

iphone MPMoviePlayerViewController : extract total duration


how can i get the video'a total time, before it plays the video in MPMoviePlayerViewController?


Solution

  • To get total duration of movie you can use :

    1). Use AVPlayerItem class and AVFoundation and CoreMedia framework. (I have used UIImagePickerController for picking the movie)

    #import <AVFoundation/AVFoundation.h>
    #import <AVFoundation/AVAsset.h>
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
    
        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];
    
        CMTime duration = playerItem.duration;
        float seconds = CMTimeGetSeconds(duration);
        NSLog(@"duration: %.2f", seconds);
    }
    

    2). MPMoviePlayerController has a property duration .Refer Apple Doc