谁能指导我,如何从url本地存储m3u8文件,以便以后离线播放。EXT-X-ALLOW-CACHE能帮我解决这个问题吗?
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
NSLog(@"connection %@",connection);`
我可以流&直接从url播放m3u8。但无法将其存储在本地并解析。ts文件并播放…请指导。
第一步:下载文件&保存
NSError *err = nil;
NSString *url = [[NSString stringWithFormat:@"http://www.example.com/playlist.m3u8"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *the_file = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
if(err != nil) {
// do some error handling, e.g. NSLog(@"Fail: %@", [err localizedDescription]);
} else {
// save the file ( see Step 2 )
}
步骤2:保存文件
可以另存为文件,也可以另存为对象。
// save as file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/playlist.m3u8", docDirectory];
[the_file writeToFile:fileName atomically:NO encoding:NSASCIIStringEncoding error:nil];
// OR, save as object
[[NSUserDefaults standardUserDefaults] setObject:the_file forKey:@"MY_M3U8"];
步骤3:读取文件
// read from file
NSString *textPath = [docDirectory stringByAppendingPathComponent:@"playlist.m3u8"];
// the FILE is loaded
// OR, read from object
NSString *content = [[NSUserDefaults standardUserDefaults] stringForKey:@"MY_M3U8"];
// it will contain the CONTENT of file
步骤4:在播放器中播放
你应该做这件事。不需要我写,对吧?