如何播放沙盒里的m3u8


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

1换怖、在沙盒里搭建本地虛擬服務(wù)器

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

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

一届氢、搭建沙盒虛擬服務(wù)器

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

1、創(chuàng)建工程退子,導(dǎo)入GCDWebServer

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

2贱迟、搭建虛擬服務(wù)器姐扮,并啟動

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

//判斷沙盒下是否存在衣吠,把工程的文件復(fù)制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(@"%@.%@成功復(fù)制到沙盒", fileName, fileType);

}else {

NSLog(@"%@.%@復(fù)制到沙盒失敗", 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


四、運行效果

五缚俏、最后附上網(wǎng)盤源碼鏈接

經(jīng)小伙伴指出惊搏,在真機(jī)上無法播放,查了下原因是忧换,代碼里webServer的端口寫的默認(rèn)的恬惯,應(yīng)該是被占用了,因此就換了個端口亚茬,更新了下demo

新的demo源碼

github-demo鏈接


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末酪耳,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子刹缝,更是在濱河造成了極大的恐慌碗暗,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梢夯,死亡現(xiàn)場離奇詭異言疗,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)颂砸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門噪奄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人人乓,你說我怎么就攤上這事勤篮。” “怎么了色罚?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵碰缔,是天一觀的道長。 經(jīng)常有香客問我保屯,道長手负,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任姑尺,我火速辦了婚禮竟终,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘切蟋。我一直安慰自己统捶,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著喘鸟,像睡著了一般匆绣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上什黑,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天崎淳,我揣著相機(jī)與錄音,去河邊找鬼愕把。 笑死拣凹,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的恨豁。 我是一名探鬼主播嚣镜,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼橘蜜!你這毒婦竟也來了菊匿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤计福,失蹤者是張志新(化名)和其女友劉穎跌捆,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棒搜,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡疹蛉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年活箕,在試婚紗的時候發(fā)現(xiàn)自己被綠了力麸。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡育韩,死狀恐怖克蚂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情筋讨,我是刑警寧澤埃叭,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站悉罕,受9級特大地震影響赤屋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜壁袄,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一类早、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嗜逻,春花似錦涩僻、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嵌巷。三九已至,卻和暖如春室抽,著一層夾襖步出監(jiān)牢的瞬間搪哪,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工坪圾, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留噩死,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓神年,卻偏偏與公主長得像已维,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子已日,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容