---
title: "iOS Dev (24) 最简单的M3U8播放器"
author: "prevention"
date: "2014-01-09T15:26:00Z"
tags: []
slug: "ios-dev-24-m3u8"
draft: false
lastmod: "2014-02-03T16:56:18Z"
id: 2010870
url: "https://prevention.iteye.com/blog/2010870"
---

# iOS Dev (24) 最简单的M3U8播放器

- 作者：CSDN 大锐哥
- 地址：[http://blog.csdn.net/prevention](http://blog.csdn.net/prevention)

## 概述

用 MediaPlayer Framework 中的 MPMoviePlayerController 构造一个最简单的 M3U8 播放器。

## Show Me the Codes

创建一个空项目，然后改写 AppDelegate：

### AppDelegate.h

```
#import <UIKit/UIKit.h>

@class PlayerViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) PlayerViewController *vc;

@end
```

### AppDelegate.m

```
#import "AppDelegate.h"
#import "PlayerViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary    *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.vc = [[PlayerViewController alloc] initWithNibName:nil bundle:nil];
    self.window.rootViewController = self.vc;
    [self.window makeKeyAndVisible];
    return YES;
}

...

@end
```

其他默认函数我就略了。

### PlayerViewController.h

```
#import <UIKit/UIKit.h>

@interface PlayerViewController : UIViewController

@end
```

### PlayerViewController.m

在 viewDidLoad 中初始化 MPMoviePlayerController，并指定一个播放地址。这里我写死了地址，是为了演示。

```
#import "PlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface PlayerViewController ()

@property (strong, nonatomic) MPMoviePlayerController *streamPlayer;

@end

@implementation PlayerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *streamURL = [NSURL URLWithString:@"http://www.thumbafon.com/code_examples/video/segment_example/prog_index.m3u8"];

    self.streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
    [self.streamPlayer.view setFrame:self.view.bounds];
    self.streamPlayer.controlStyle = MPMovieControlStyleEmbedded;

    [self.view addSubview: self.streamPlayer.view];
    [self.streamPlayer play];
}

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

@end
```

Run It !

\-

转载请注明来自：[http://blog.csdn.net/prevention](http://blog.csdn.net/prevention)