iOS-如何使用AVAsset或AVURLAsset获取.mp4文件的持续时间 [英] iOS- how to get the duration of .mp4 file by using AVAsset or AVURLAsset
问题描述
我知道视频类型问题的持续时间之前已经得到解答但我在使用 AVAsset获取
和 .mp4
文件的持续时间时遇到了麻烦 AVURLAsset
。我正在使用以下代码
i know duration of video type questions have been answered before but i am facing real trouble in getting duration of an .mp4
file by using AVAsset
and by AVURLAsset
. i am using Following code
NSString *itemPathString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:obmodel.actualname];
NSURL *itemPathURL = [NSURL URLWithString:itemPathString];
if([[NSFileManager defaultManager] fileExistsAtPath:itemPathString]){
NSLog(@"File Exists");
}
AVAsset *videoAsset = (AVAsset *)[AVAsset assetWithURL:itemPathURL];
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
NSLog(@"duration: %.2f", CMTimeGetSeconds(videoAssetTrack.timeRange.duration));
if(CMTimeGetSeconds(videoAsset.duration) >= 59.0){
}
但每次返回 0
i还导入了标题
#import <AVFoundation/AVAsset.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
我也尝试过其他一些我知道的代码但是没有人能为我工作。
如果我通过 iExplorer
播放我的视频工作正常,我的路径很好,但不知道我什么都不知道。
i希望分享我发现的是我的 AVAssets
没有正确加载
请建议我应该做什么。
i have also tried some other code that i know but neither one worked for me.
My video is working properly if I play it through iExplorer
, my Path is good but don't know whats i'm missing.
i would like to share that what i have found is that my AVAssets
are not loaded properly
please suggest me what should be done.
推荐答案
是的,之前我遇到过类似的问题。对我来说,当我使用时,问题就消失了:
Yes, I've had similar problems before. For me the problem went away when I used:
NSURL *itemPathURL = [NSURL fileURLWithPath:itemPathString];
而不是
NSURL *itemPathURL = [NSURL URLWithString:itemPathString];
如果您完全确定自己的路径没问题,这可以解决您的问题。
If you're completely sure your path is ok, this should fix your problem.
希望这会有所帮助!
这篇关于iOS-如何使用AVAsset或AVURLAsset获取.mp4文件的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!