AVPlayer播放远程视频

导入头文件

#import 

播放器属性

/** 播放器*/
@property (nonatomic, strong) AVPlayer *player;

播放器的懒加载

- (AVPlayer *)player
{
    if (!_player) {
        //本地/远程
        _player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"]]];
        AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
        //宽高比 注意是 16 : 9
        layer.frame = CGRectMake(0, 0, 300, 200 * 9 / 16.0);
        //添加图层
        [self.view.layer addSublayer:layer];
    }
    return _player;
}

点击屏幕开始播放

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.player play];
}

注意,想要实现类似播放器的进度控制,点击全屏等需要自己基于AVPlayer封装一个,后面会把封装的代码贴上.

你可能感兴趣的:(AVPlayer播放远程视频)