第一种方法
NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *urlAsset = [AVURLAssetURLAssetWithURL:videoUrloptions:opts];
NSInteger totalSecond = urlAsset.duration.value / urlAsset.duration.timescale;
使用上述代码需要插入AVFoundation框架。播过我用这种方法计算视频长度时,会延迟其后代码的执行,不知道什么原因。
第二种方法
使用MPMoviePlayerController的duration属性,当MPMoviePlayerController的duration未知时默认为0.0,如果duration确定,会发送MPMovieDurationAvailableNotification通知,接着duration的值将会更新为播放视频的长度,单位是秒。