iOS- how to get the duration of .mp4 file by using AVAsset or AVURLAsset

試著忘記壹切 提交于 2019-12-07 06:14:01

问题


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){
}

but every time it returns 0 i have also imported following headers

#import <AVFoundation/AVAsset.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>

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.


回答1:


Yes, I've had similar problems before. For me the problem went away when I used:

NSURL *itemPathURL = [NSURL fileURLWithPath:itemPathString];

instead of

NSURL *itemPathURL = [NSURL URLWithString:itemPathString];

If you're completely sure your path is ok, this should fix your problem.

Hope this helps!



来源:https://stackoverflow.com/questions/29255776/ios-how-to-get-the-duration-of-mp4-file-by-using-avasset-or-avurlasset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!