2012-01-18 66 views

回答

7

要获得电影的总时长,您可以使用:

1)。使用AVPlayerItem类和AVFoundationCoreMedia framework。 (我用UIImagePickerController来拍摄电影)

#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有一个属性duration .Refer Apple Doc

+0

在MPMoviePlayerController中,在我们真正播放视频之前,持续时间为零。如果你得到任何方式,PLZ添加代码 – 2012-01-18 13:25:14

+0

它代表目前已被缓冲的持续时间,而不是总持续时间。 – Maulik 2012-01-18 13:30:24

+0

第一个选项确实成功了,非常感谢。你创造了一天...... :) – 2012-01-19 06:59:54

1

如果你不想得到MPMoviePlayerViewController的duration财产的总时间(因为这带来了电影播放器​​UI),您可以创建带有通过文件URL传递您的视频文件的AVAsset对象,然后检查那就是duration属性。

这个技巧只适用于iOS 5(这是AVAsset的assetWithURL:来的地方)。

3

得到总的持续时间在MPMoviePlayerViewController

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

注:以上代码给相关媒体的总时间以秒为

0

你气可以快速使用此方法

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

     return CMTimeGetSeconds(duration) 
    }