如何播放沙盒里的m3u8


播放沙盒里的m3u8流,大致流程為:

1、在沙盒里搭建本地虛擬服務器

2、下載m3u8文件到沙盒(本文采取把已下載好的流拷貝到沙盒里)

3、以虛擬host+port的方式,播放m3u8流

一、搭建沙盒虛擬服務器

我們借助開源項目GCDWebServer,附上github鏈接GCDWebServer

1、創建工程,導入GCDWebServer

pod 'GCDWebServer/WebDAV', '~> 3.3.3'

2、搭建虛擬服務器,并啟動

#import "AppDelegate.h"

#import "GCDWebDAVServer.h"

@interface AppDelegate ()

@property (nonatomic, strong) GCDWebDAVServer* davServer;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

_davServer = [[GCDWebDAVServer alloc] initWithUploadDirectory:documentsPath];

[_davServer start];

NSLog(@"serverURL:%@", _davServer.serverURL);

return YES;

}

二、拷貝m3u8流到沙盒里

拷貝m3u8索引文件及其ts切片到沙盒document目錄里

#import "ViewController.h"

#import "GCDWebDAVServer.h"

#import "PlayerViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.backgroundColor = [UIColor redColor];

btn.frame = CGRectMake(100, 100, 50, 50);

[btn setTitle:@"click" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(bntClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

[self copyFileToDocument:@"testVideo" type:@"m3u8"];

[self copyFileToDocument:@"testVideo-0" type:@"ts"];

[self copyFileToDocument:@"testVideo-1" type:@"ts"];

}

- (void)bntClick{

PlayerViewController *player = [[PlayerViewController alloc] init];

[self presentViewController:player animated:YES completion:nil];

}

- (void)copyFileToDocument:(NSString*)fileName type:(NSString *)fileType{

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",fileName, fileType]];

NSFileManager *fm = [NSFileManager defaultManager];

//判斷沙盒下是否存在,把工程的文件復制document目錄下

BOOL isExist = [fm fileExistsAtPath:filePath];

if (!isExist){

//獲取工程中文件

NSString *fileBundlePath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileType];

if ([fm copyItemAtPath:fileBundlePath toPath:filePath error:nil]) {

NSLog(@"%@.%@成功復制到沙盒", fileName, fileType);

}else {

NSLog(@"%@.%@復制到沙盒失敗", fileName, fileType);

}

} else {

NSLog(@"%@.%@已存在沙盒里", fileName, fileType);

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


三、開始播放

采取最簡單的AVPlayer來播放

#import "PlayerViewController.h"

@interface PlayerViewController ()

@property (nonatomic, strong) AVPlayerLayer *playerLayer;

@property (nonatomic, strong) AVPlayer *player;

@property (nonatomic, strong) AVPlayerItem *playerItem;

@end

@implementation PlayerViewController

- (void)viewDidLoad

{

[super viewDidLoad];

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testVideo.m3u8"];

NSFileManager *fm = [NSFileManager defaultManager];

if ([fm fileExistsAtPath:filePath]) {

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/testVideo.m3u8"]];

self.playerItem = [AVPlayerItem playerItemWithURL:url];

self.player = [AVPlayer playerWithPlayerItem:self.playerItem];

self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];

self.playerLayer.frame = self.view.bounds;

self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;

[self.view.layer addSublayer:self.playerLayer];

[self.player play];

}else {

NSLog(@"文件不存在");

}

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


四、運行效果

五、最后附上網盤源碼鏈接

經小伙伴指出,在真機上無法播放,查了下原因是,代碼里webServer的端口寫的默認的,應該是被占用了,因此就換了個端口,更新了下demo

新的demo源碼

github-demo鏈接


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容