1.獲取Caches 文件夾路徑遍略。
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog: ? ?/Users/sundongri/Library/Developer/CoreSimulator/Devices/649D1D25-FA3F-4EB2-96FC-92AF6A6B9D97/data/Containers/Data/Application/5668FC61-F42D-4C07-9761-24577C5327CE/Library/Caches
2.在Caches 文件夾下面 建一個子文件夾便于管理宦赠!
NSString *imagePath = [NSString stringWithFormat:@"%@/Media", pathDocuments];
[[NSFileManager defaultManager] createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
NSLog: ?/Users/sundongri/Library/Developer/CoreSimulator/Devices/649D1D25-FA3F-4EB2-96FC-92AF6A6B9D97/data/Containers/Data/Application/5668FC61-F42D-4C07-9761-24577C5327CE/Library/Caches/Media
文件名字為Media。
3.獲取NSData
//工程里的視頻
NSString * path = [[NSBundle mainBundle] pathForResource:@"D0024" ofType:@"mp4"];
//轉(zhuǎn)化成NSData
NSData *videoMy=[NSData dataWithContentsOfFile:path];
//初始化一個動態(tài)的data 容器?
NSMutableData *videoData =[NSMutableData data];
//初始化要用這種? 不要用videoData=nil;
[videoData setData:[NSData data]];
//添加Data 也可以用來接收下載過來的Data勾扭,因為下載的數(shù)據(jù)是一段一段過來的 所以appendData 不斷的加(本地的數(shù)據(jù)就一次行加進(jìn)來了)
[videoData appendData:videoMy];
//imagePath 是咱們創(chuàng)建的media文件夾拼上DJI_0024.mp4 毡琉,最后結(jié)尾為media/DJI_0024.mp4
NSString *filePath = [imagePath stringByAppendingPathComponent:@"DJI_0024.mp4"];
//接下來就是寫進(jìn)文件 然后播放本地文件
BOOL isSuccess = [videoData writeToFile:filePath atomically:YES];
if (isSuccess) {
NSLog(@"能寫進(jìn)去");
}
else{
NSLog(@"不成功");
}
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayerViewController * play = [[AVPlayerViewController alloc]init];
play.player = [[AVPlayer alloc]initWithURL:url];
[self presentViewController:play animated:YES completion:nil];