简单AVPlayer播放

import "ViewController.h"

import

@interface ViewController ()

@property(nonatomic, strong)AVPlayer *videoPlayer;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // 创建button 点击按钮播放
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor blackColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(btnButton:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)btnButton:(UIButton *)button{

NSURL *url = [NSURL URLWithString:@"http://baobab.wdjcdn.com/1461897495660000111.mp4"];


AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:url];
// 初始化videoPlayer
self.videoPlayer = [[AVPlayer alloc]initWithPlayerItem:item];
// 把视频层放到layer上
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];

layer.frame = self.view.frame;

// 居中
layer.videoGravity = AVLayerVideoGravityResizeAspect;


// 添加到播放视频上
[self.view.layer addSublayer:layer];


// 全屏播放
layer.bounds = CGRectMake(0, 0, self.view.frame.size.height, CGRectGetWidth(self.view.frame));

// 播放效果
layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1);

// 开始播放
[self.videoPlayer play];

}

你可能感兴趣的:(简单AVPlayer播放)