第一个ios上的Native Avplayer demo

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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。