播放本地m3u8流媒體
阿新 • • 發佈:2019-02-02
//// M3u8DownloadManager.m// DownloadM3u8////#import"M3u8DownloadManager.h"
#import "URLConnectionGet.h"#import<M3U8Kit.h>
#import <NSString+m3u8.h>@implementation M3u8DownloadManager
{
NSMutableString *m3u8List;
NSString *fileCompoment;
NSString *fileFolder;
NSMutableArray *urls;
NSMutableArray *names;
}
- ( id)init
{
self = [superinit];
if (self) {
urls = [[NSMutableArrayalloc]init];
names = [[NSMutableArrayalloc]init];
}
returnself;
}
//根據檔案路徑下載.ts檔案以及建立m3u8列表
- (void)downloadM3u8WithFile:(NSString *)URL
{
fileCompoment = URL.lastPathComponent;
[[[ URLConnectionGetalloc]init]downloadDataWithUrlString:URLfinished:^(NSData
*data) {
//下載得到m3u8列表m3u8List = [[NSMutableStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
if (m3u8List) {
[selfparser:m3u8ListwithURL:URL];
}
} failed :^(NSString
*errorMessage) {
NSLog(@"列表下載失敗");
}];
}
//解析m3u8列表
- (void)parser:(NSString *)txt withURL:(NSString *)URL
{
M3U8SegmentInfoList *list = [txtm3u8SegementInfoListValueRelativeToURL:[NSURLURLWithString:URL]];
NSLog(@"%ld",list.count);
//解析列表for (int i = 0; i < list.count; i++) {
M3U8SegmentInfo *info = [list segmentInfoAtIndex:i];
NSLog(@"%@",info.mediaURL);
//獲取檔名//檔名
[names addObject:info.URI];
//整個請求地址
NSString *pre = [URL stringByReplacingOccurrencesOfString:URL.lastPathComponent withString:@""];
NSString *tsUrl = [NSString stringWithFormat:@"%@%@",pre,info.URI];
[urls addObject:tsUrl];
}
//建立一個伺服器下的檔名對應的資料夾
NSArray *items = [URL.lastPathComponent componentsSeparatedByString:@"."];
fileFolder = [NSString stringWithFormat:@"%@/Documents/web/%@",NSHomeDirectory(),items[0]];
NSFileManager *fm = [NSFileManager defaultManager];
[fm createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:nil];
//建立m3u8列表
[self createM3u8ListFile:[fileFolder copy]];
NSLog(@"%@",fileFolder);
//開始下載
[self downloadTS:[fileFolder copy]];
}
- (void)downloadTS:(NSString *)savePath
{
if (urls.count>0) {
[self requestWithIndex:0 savePath:savePath];
}
}
- (void)requestWithIndex:(int)index savePath:(NSString *)savePath
{
__blockint blockIndex = index;
__weak NSMutableArray *weakNames = names;
__weak NSString *weakSavePath = savePath;
__weak M3u8DownloadManager *weakSelf =self;
[[URLConnectionGet alloc] downloadDataWithUrlString:urls[blockIndex] finished:^(NSData *data) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@",weakSavePath,weakNames[blockIndex]];
NSMutableData *currentData = [NSMutableData dataWithContentsOfFile:filePath];
if (currentData ==nil) {
currentData = [[NSMutableData alloc] init];
}
[currentData appendData:data];
BOOL success = [currentData writeToFile:filePath atomically:YES];
if (success) {
blockIndex += 1;
if (blockIndex < urls.count) {
[weakSelf requestWithIndex:blockIndex savePath:weakSavePath];
}
}
} failed:^(NSString *errorMessage) {
NSLog(@"%@",errorMessage);
}];
}
//建立m3u8列表
- (void)createM3u8ListFile:(NSString *)savePath
{
for (int i = 0; i < urls.count; i++) {
NSString *fileName = names[i];
NSString *URI = [NSString stringWithFormat:@"%@/%@",savePath,fileName];
[m3u8List stringByReplacingOccurrencesOfString:fileName withString:URI];
}
//將m3u8寫到資料夾
[self writeM3u8List];
}
- (void)writeM3u8List
{
NSData *data = [m3u8List dataUsingEncoding: NSUTF8StringEncoding];
NSString *fileName = [fileCompoment componentsSeparatedByString:@"."][0];
NSString *filePath = [NSString stringWithFormat:@"%@/Documents/web/%@/%@",NSHomeDirectory(),fileName,fileCompoment];
BOOL is = [data writeToFile:filePath atomically:YES];
NSLog(@"%d",is);
}
#import "URLConnectionGet.h"#import<M3U8Kit.h>
#import <NSString+m3u8.h>@implementation M3u8DownloadManager
{
NSMutableString *m3u8List;
NSString *fileCompoment;
NSString *fileFolder;
NSMutableArray *urls;
NSMutableArray *names;
}
- (
{
self = [superinit];
if (self) {
urls = [[NSMutableArrayalloc]init];
names = [[NSMutableArrayalloc]init];
}
returnself;
}
//根據檔案路徑下載.ts檔案以及建立m3u8列表
- (void)downloadM3u8WithFile:(NSString *)URL
{
fileCompoment = URL.lastPathComponent;
[[[
//下載得到m3u8列表m3u8List = [[NSMutableStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
if (m3u8List) {
[selfparser:m3u8ListwithURL:URL];
}
} failed
NSLog(@"列表下載失敗");
}];
}
//解析m3u8列表
- (void)parser:(NSString *)txt withURL:(NSString *)URL
{
M3U8SegmentInfoList *list = [txtm3u8SegementInfoListValueRelativeToURL:[NSURLURLWithString:URL]];
NSLog(@"%ld",list.count);
//解析列表for (int i = 0; i < list.count; i++) {
M3U8SegmentInfo *info = [list segmentInfoAtIndex:i];
NSLog(@"%@",info.mediaURL);
//獲取檔名//檔名
[names addObject:info.URI];
//整個請求地址
NSString *pre = [URL stringByReplacingOccurrencesOfString:URL.lastPathComponent withString:@""];
NSString *tsUrl = [NSString stringWithFormat:@"%@%@",pre,info.URI];
[urls addObject:tsUrl];
}
//建立一個伺服器下的檔名對應的資料夾
NSArray *items = [URL.lastPathComponent componentsSeparatedByString:@"."];
fileFolder = [NSString stringWithFormat:@"%@/Documents/web/%@",NSHomeDirectory(),items[0]];
NSFileManager *fm = [NSFileManager defaultManager];
[fm createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:nil];
//建立m3u8列表
[self createM3u8ListFile:[fileFolder copy]];
NSLog(@"%@",fileFolder);
//開始下載
[self downloadTS:[fileFolder copy]];
}
- (void)downloadTS:(NSString *)savePath
{
if (urls.count>0) {
[self requestWithIndex:0 savePath:savePath];
}
}
- (void)requestWithIndex:(int)index savePath:(NSString *)savePath
{
__blockint blockIndex = index;
__weak NSMutableArray *weakNames = names;
__weak NSString *weakSavePath = savePath;
__weak M3u8DownloadManager *weakSelf =self;
[[URLConnectionGet alloc] downloadDataWithUrlString:urls[blockIndex] finished:^(NSData *data) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@",weakSavePath,weakNames[blockIndex]];
NSMutableData *currentData = [NSMutableData dataWithContentsOfFile:filePath];
if (currentData ==nil) {
currentData = [[NSMutableData alloc] init];
}
[currentData appendData:data];
BOOL success = [currentData writeToFile:filePath atomically:YES];
if (success) {
blockIndex += 1;
if (blockIndex < urls.count) {
[weakSelf requestWithIndex:blockIndex savePath:weakSavePath];
}
}
} failed:^(NSString *errorMessage) {
NSLog(@"%@",errorMessage);
}];
}
//建立m3u8列表
- (void)createM3u8ListFile:(NSString *)savePath
{
for (int i = 0; i < urls.count; i++) {
NSString *fileName = names[i];
NSString *URI = [NSString stringWithFormat:@"%@/%@",savePath,fileName];
[m3u8List stringByReplacingOccurrencesOfString:fileName withString:URI];
}
//將m3u8寫到資料夾
[self writeM3u8List];
}
- (void)writeM3u8List
{
NSData *data = [m3u8List dataUsingEncoding: NSUTF8StringEncoding];
NSString *fileName = [fileCompoment componentsSeparatedByString:@"."][0];
NSString *filePath = [NSString stringWithFormat:@"%@/Documents/web/%@/%@",NSHomeDirectory(),fileName,fileCompoment];
BOOL is = [data writeToFile:filePath atomically:YES];
NSLog(@"%d",is);
}