关于 AVPlayer的总结-1

关于 AVPlayer的总结-1

今天在使用 Xcode7.3的时候,发现 MoviePlayer 已经在 iOS9.0被弃用了,所以回头复习一下 AVPlayer。
首先 AVPlayer是在 AVFoundation/AVFoundation.h 框架下,所以首先引入头文件#import

- (void)prepareToPlay{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ThinkDiff.3gp" ofType:nil];

//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];//注意:播放本地文件路径用 fileURLWithPath
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://otmv.alicdn.com/new/mv_1_6/23/77/2306cd13e3dde338b53f404ece43a277.mp4?k=7125c1a8f09b70bb&t=1451830866"]];//创建一个网络播放源

    self.player = [AVPlayer playerWithPlayerItem:item];
//    [self.player play];
}

- (void)createUI{

    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];//关联AVPlayer 来展示视频画面信息的类
    layer.frame = CGRectMake(20, 20, self.view.frame.size.width-40, 300);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [layer setBackgroundColor:([UIColor colorWithPatternImage:[UIImage imageNamed:@"2.jpg"]]).CGColor];
    [self.view.layer addSublayer:layer];

//    设置视频画面填充模式
    [layer setVideoGravity:AVLayerVideoGravityResizeAspect];
}
- (IBAction)playOrPause:(id)sender {

    static int i = 0;

    if (i == 0) {

        [self.player play];

        i = 1;
    }else{

        [self.player pause];

        i = 0;
    }

}

最后,本人总结出来的注意事项:
//总结:注意:(1)播放源的路径,最后都转成 URL 形式,但是本地的用 fileUrlWithPath;
//(2)设置 layer 的背景颜色可以有两种,1是直接设置.backgroundColor=[UIColor ];2是设置为图片的背景色,需要加.CGColor
//(3)网络请求时注意设置 plist 文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值