关于 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 文件

你可能感兴趣的:(ios,视频播放)