实际上你可以直接用AVPlayer,也可以用AVPlayerViewController,这两者很类似,不过,AVPlayerViewController优化的更好
首先呢,来说说AVPlayer
-(AVPlayer *)player{
if (!_player) {
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.mp4"];
NSURL* url = [NSURL fileURLWithPath:pathToMovie];
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
_player = player;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.layer.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.view.layer addSublayer:playerLayer];
}
return _player;
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];这个很关键,监测播放到了结尾
-(void)playerItemDidReachEnd:(NSNotification*)notification{
_playView.hidden = NO;
[_player seekToTime:CMTimeMake(0, 1)];
[_player play];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //这样在播放完毕后才会继续有界面不然就是黑屏,如果是AVPlayerViewController,就不需要这个,所以我觉得AVPlayerViewController更好用
[_player pause];
});
}
然后呢说说AVPlayerViewController 代码差不多
- (void)viewDidLoad {
[super viewDidLoad];
[self.rightButton setTitle:NSLocalizedString(@"save", nil) forState:UIControlStateNormal];
AVPlayerViewController* controller = [[AVPlayerViewController alloc] init];
_playerViewController = controller;
AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL fileURLWithPath:MovieFromAlumbPath]];
//通过AVPlayerItem创建AVPlayer
self.player = [[AVPlayer alloc]initWithPlayerItem:item];
//给AVPlayer一个播放的layer层
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.frame = CGRectMake(0, self.naviBar.height, self.view.width, self.view.height - self.naviBar.height);
layer.backgroundColor = [UIColor blackColor].CGColor;
//设置AVPlayer的填充模式
layer.videoGravity = AVLayerVideoGravityResizeAspect;
_playerLayer = layer;
[self.view.layer addSublayer:layer];
//设置AVPlayerViewController内部的AVPlayer为刚创建的AVPlayer
controller.player = self.player;
//关闭AVPlayerViewController内部的约束
controller.view.translatesAutoresizingMaskIntoConstraints = YES;
UIImageView* playView = [[UIImageView alloc] init];
[self.view addSubview:playView];
[playView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.height.width.equalTo(@(60));
}];
UIView* tapView = [[UIView alloc] init];
[self.view addSubview:tapView];
[tapView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.naviBar.mas_bottom);
make.left.right.bottom.equalTo(self.view);
}];
playView.image = [UIImage imageNamed:@"play_icon"];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(play)];
[tapView addGestureRecognizer:tap];
// playView.userInteractionEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
_playView = playView;
// Do any additional setup after loading the view.
}
同样的
-(void)playerItemDidReachEnd:(NSNotification*)notification{
_playView.hidden = NO;
[_playerViewController.player seekToTime:CMTimeMake(0, 1)];
}
-(void)play{
if (_playView.hidden == NO) {
_playView.hidden = YES;
[_playerViewController.player play];
}else{
_playView.hidden = NO;
[_playerViewController.player pause];
}
}