//引入框架
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
//属性
@property(nonatomic,strong)AVPlayer * player;
@property(nonatomic,strong)AVPlayerViewController * playerView;
NSString *playString = @"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
//视频播放的url
NSURL *playerURL = [NSURL URLWithString:playString];
//初始化
self.playerView = [[AVPlayerViewController alloc]init];
//AVPlayerItem 视频的一些信息 创建AVPlayer使用的
AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:playerURL];
//通过AVPlayerItem创建AVPlayer
self.player = [[AVPlayer alloc]initWithPlayerItem:item];
//给AVPlayer一个播放的layer层
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);
layer.backgroundColor = [UIColor greenColor].CGColor;
//设置AVPlayer的填充模式
layer.videoGravity = AVLayerVideoGravityResize;
[self.view.layer addSublayer:layer];
//设置AVPlayerViewController内部的AVPlayer为刚创建的AVPlayer
self.playerView.player = self.player;
//关闭AVPlayerViewController内部的约束
self.playerView.view.translatesAutoresizingMaskIntoConstraints = YES;
//开始播放
[self showViewController:self.playerView sender:nil];
网友评论