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 <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<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这一组的视频接口,文件比较大,大家可以用一个比较小的播放地址
    NSString * str = @"http://www.demaxiya.com/app/index.php?m=play&vid=29456&quality=2&rand_2=2";

   
 //  第三方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

你可能感兴趣的:(一些实用技术,iOS,开发)