AVPlayerViewController

作者: fwlong | 来源:发表于2016-06-24 20:34 被阅读235次
//引入框架
#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];

相关文章

网友评论

  • 哇次哟累:给你差评,运行处问题.而且,说白了还不是在用AVPlayer,还不如就直接用AVPlayer算了
    fwlong:@哇次哟累 哦,谢谢。其实就是用AVplayer。这个只是我当时的想法,记录下来了

本文标题:AVPlayerViewController

本文链接:https://www.haomeiwen.com/subject/traodttx.html