Reputation: 1302
how can i get the video'a total time, before it plays the video in MPMoviePlayerViewController?
Upvotes: 4
Views: 4181
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
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
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
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