1、最简单的APP
先定义几个必须的变量:
#import "AVFoundation/AVFoundation.h"
@property (strong, nonatomic) UIView *backView;
@property (nonatomic,strong) AVPlayer *player;
@property (nonatomic,strong) AVPlayerItem *playerItem;
@property (nonatomic,strong) AVPlayerLayer *playerLayer;
2、显示当前
//构建ui
self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, kScreenWidth, kScreenHeight / 2.5)];
self.backView.backgroundColor = [UIColor clearColor];//clearColor
[self.view addSubview:self.backView];
// 初始化播放器item
self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://1111.m3u8"]];
self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
// 初始化播放器的Layer
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.frame = self.backView.bounds;
[self.backView.layer insertSublayer:self.playerLayer atIndex:0];
//播放
[self.player play];
3、最后一个设置info.plist
播放网络地址时 在info.plist中添加 App Transport Security Settings字典中添加Allow Arbitrary Loads元素 值为YES
4009159-f36b60fb1b2308de.png
ps:
NSURL *url = [NSURL URLWithString:@"http://stream1.visualon.com:8082/hls/v10/gear/bipbop_16x9_variant_v10_2.m3u8"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];