iOS M3U8视频的下载与播放

用的第三方有CocoaHTTPServer,ASIHTTPRequest2
//
//  AppDelegate.h
//  TestM3U8
//
//  Created by  王聪  on 14-3-14.
//  Copyright (c) 2014   王聪 . All rights reserved.
//

#import 
#import
  "HTTPServer.h"
@interface  AppDelegate :  UIResponder <</span>UIApplicationDelegate>
{
   
 HTTPServer *httpServer;
}
@property (strong, nonatomic) UIWindow *window;

@end

//
//  AppDelegate.m
//  TestM3U8
//
//  Created by on 14-3-14.
//  Copyright (c) 2014 . All rights reserved.
//

#import "AppDelegate.h"
#define kPathDownload @
"Downloads"

@implementation AppDelegate

- (
BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 
   
 //  m3u8文件下载好之后,得搭建本地服务器
   
 httpServer = [[HTTPServer alloc] init];

//  设置服务器类型为tcp
[
httpServer setType:@"_http._tcp."];

   
 //  设置本地服务器端口号,你可以随意设置端口号
    [
httpServer setPort:12345];
   
   
 NSString *pathPrefix = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
   
   
   
 NSString *webPath = [pathPrefix stringByAppendingPathComponent:kPathDownload];
NSLog(@"Setting document root: %@", webPath);
//  指定本地服务器播放的文件路径
[
httpServer setDocumentRoot:webPath];

   
NSError *error;
if(![httpServer start:&error])
{
NSLog(@"Error starting HTTP Server: %@", error);
}
   
   
   
 // Override point for customization after application launch.
   
 return YES;
}

@end
//
//  ViewController.h
//  TestM3U8
//
//  Created by 侯志超 on 14-3-14.
//  Copyright (c) 2015 侯志超. All rights reserved.
//

#import 
#import
 "M3U8Handler.h"
#import
 "VideoDownloader.h"
@interface ViewController :UIViewController<</span>M3U8HandlerDelegate,VideoDownloadDelegate>

@end

//
//  ViewController.m
//  TestM3U8
//
//  Created by 王聪  on 14-3-14.
//  Copyright (c) 2014  王聪. All rights reserved.
//

#import "ViewController.h"
#import
 "M3U8Handler.h"
#import
 "M3U8Playlist.h"
#import
 "VideoDownloader.h"
#import
 
@interface ViewController ()

@end

@implementation ViewController

- (
void)viewDidLoad
{
    [
super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

//  这个方法是利用ASIm3u8文件中的地址分别进行请求数据,变成分段的视频
-(
void)praseM3U8Finished:(M3U8Handler*)handler
{
    handler.
playlist.uuid = @"moive5";
   
 VideoDownloader *downloader = [[VideoDownloaderalloc]initWithM3U8List:handler.playlist];
    downloader.
delegate = self;
    [downloader
 startDownloadVideo];
}
//  全部片段都下载好之后的回调方法
-(
void)videoDownloaderFinished:(VideoDownloader*)request
{
   
 //  创建本地播放的m3u8文件
    [request
 createLocalM3U8file];
   
 NSLog(@"everything ok");
}
- (
IBAction)start:(id)sender {
   
    //  用的是The word's key这一组的视频接口,文件比较大,大家可以用一个比较小的播放地址

   
 //  第三方m3u8解析
   
 M3U8Handler *handler = [[M3U8Handler alloc] init];
    handler.
delegate = self;
   
   
 //        m_webView.delegate = nil;
   
 //解析urlm3u8地址
    [handler
 praseUrl:str];
}
- (
IBAction)play:(id)sender {
   
 //  播放创建好的本地m3u8文件
   
 MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURLURLWithString:@"http://127.0.0.1:12345/moive5/movie.m3u8"]];
   
    [playerViewController.
view setFrame:CGRectMake(0, 0, 320,568)];
    [
self.view addSubview:playerViewController.view];
    [playerViewController.
moviePlayer play];
}

- (
void)didReceiveMemoryWarning
{
    [
super didReceiveMemoryWarning];
   
 // Dispose of any resources that can be recreated.
}

@end

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值