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

本文指导您使用MediaPlayerFramework中的MPMoviePlayerController快速搭建一个简单的M3U8播放器,通过创建一个空项目并修改AppDelegate和PlayerViewController来实现播放功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/prevention

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值