Iphone MPMoviePlayerViewController: Exposure to Total Duration

How can I get the total video time before it plays the video in MPMoviePlayerViewController?

+4
source share
4 answers

To get the total duration of a movie, you can use:

1). Use the AVPlayerItem class and the AVFoundation and CoreMedia framework . (I used UIImagePickerController to select 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 duration .Refer Apple Doc property

+7
source

To get the total duration in MPMoviePlayerViewController

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

Note. Above the code, indicate the total duration of related materials in seconds

+3
source

If you do not want to get the total time from the MPMoviePlayerViewController duration property (because it calls the movie player user interface), you can instead create an AVAsset object with your video file transferred via the file URL, and then check the duration property.

This trick will only work on iOS 5 (where AVAsset assetWithURL: :) is located.

+1
source

You can use this method in quick

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

Source: https://habr.com/ru/post/1391633/


All Articles