5

谁能指导我,如何从 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 文件并播放...请指导。

4

1 回答 1

-2

第 1 步:下载文件并保存

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"];

第三步:读取文件

// 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 步:在播放器中播放

你应该这样做的。不用我写了吧?

于 2013-03-27T08:20:57.730 回答