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

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

概述

用 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

你可能感兴趣的:(ios)