利用AVPlayerViewController实现点击cell中的button播放视频
#import <AVFoundation/AVFoundation.h>
AHUPlayerViewController.h中声明的topic是本人项目中用于取视频url等信息的数据模型
// // AHUPlayerViewController.h // Share // // Created by Apple on 2017/3/25. // Copyright © 2017年 Apple. All rights reserved. // #import <AVKit/AVKit.h> @class AHUTopic; @interface AHUPlayerViewController : AVPlayerViewController /** 模型数据 */ @property (nonatomic, strong) AHUTopic *topic; @end
AHUPlayerViewController.m文件,实现的具体操作
// // AHUPlayerViewController.m // Share // // Created by Apple on 2017/3/25. // Copyright © 2017年 Apple. All rights reserved. // #import "AHUPlayerViewController.h" #import "AHUTopic.h" #import <AVFoundation/AVFoundation.h> @interface AHUPlayerViewController () @property (nonatomic, strong) AVPlayerViewController *playerView; @end @implementation AHUPlayerViewController - (void)viewDidLoad { [super viewDidLoad]; //视频播放的url NSURL *playerURL = [NSURL URLWithString:self.topic.videouri]; //初始化 self.playerView = [[AVPlayerViewController alloc]init]; //AVPlayerItem 视频的一些信息 创建AVPlayer使用的 AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:playerURL]; //通过AVPlayerItem创建AVPlayer self.player = [[AVPlayer alloc] initWithPlayerItem:item]; //AVPlayerViewController内部的约束 self.playerView.view.translatesAutoresizingMaskIntoConstraints = NO; } @end
在videoView中监听,点击了封面图片或播放按钮都会调用seeVideo方法,弹出视频播放界面
- (void)awakeFromNib { [super awakeFromNib]; //从xib中加载进来的空间的autoresizingMask默认是UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight self.autoresizingMask = UIViewAutoresizingNone; self.imageView.userInteractionEnabled = YES; [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeVideo)]]; [self.playButton addTarget:self action:@selector(seeVideo) forControlEvents:UIControlEventTouchUpInside]; } - (void)seeVideo { AHUPlayerViewController *videoView = [[AHUPlayerViewController alloc] init]; videoView.topic = self.topic; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:videoView animated:YES completion:nil]; }