Shefali Soni
Shefali Soni

Reputation: 1302

iphone MPMoviePlayerViewController : extract total duration

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

Upvotes: 4

Views: 4181

Answers (4)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

YOu can get using this method in swift

func getMediaDuration(url: NSURL!) -> Float64{
        var asset : AVURLAsset = AVURLAsset.assetWithURL(url) as AVURLAsset
        var duration : CMTime = asset.duration

        return CMTimeGetSeconds(duration)
    }

Upvotes: 0

Hardik Thakkar
Hardik Thakkar

Reputation: 15951

To get the total Duration in MPMoviePlayerViewController

MPMoviePlayerViewController *mp;
float seconds =mp.moviePlayer.duration;

Note: Above code give the total Duration of related Media in seconds

Upvotes: 3

Maulik
Maulik

Reputation: 19418

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

Upvotes: 7

Michael Dautermann
Michael Dautermann

Reputation: 89509

If you don't want to get the total time from MPMoviePlayerViewController's duration property (because that brings up the movie player UI), you could instead create an AVAsset object with your video file passed in via a file URL and then check the duration property on that.

This trick would only work on iOS 5 (which is where AVAsset's assetWithURL: came in with).

Upvotes: 1

Related Questions