最近在仿做一个APP,其中使用到视频流播放,原本是打算使用MPPlayer进行在线播放出来的,但是发现iOS9之后以及不在支持MP了,刚好也没怎么接触过AVPlyer借此机会刚好学习一番。因为AVPlayer所有控件都是需要自定义,如果只是简单的播放视频其实是不如MPPlayer方便的。
实现过程
1. 简单点的MPPlayer
- 声明一个
MPMoviePlayerController
的属性
1 2
| @property (nonatomic, strong) MPMoviePlayerController *moviePlayer; @property (nonatomic, strong) NSString *moviePath;
|
1 2 3 4 5 6 7 8 9 10 11 12
| if (!_moviePlayer) { _moviePlayer = [[MPMoviePlayerController alloc]init]; [_moviePlayer.view setFrame:CGRectMake(0, 300, self.view.bounds.size.width, self.view.bounds.size.height/3)]; [_moviePlayer setShouldAutoplay:NO]; _moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:_moviePlayer.view]; } return _moviePlayer;
|
如果只是播放暂停之类的几乎是不需要自己做什么的,很简单。
- 设置播放源
我是在set
方法中设置的,不过这个都无所谓。
1 2 3 4 5 6 7 8
| - (void)setMoviePath:(NSString *)moviePath{ _moviePath = [moviePath copy]; self.moviePlayer.view.backgroundColor = [UIColor clearColor]; self.moviePlayer.contentURL = [NSURL URLWithString:moviePath]; [self.moviePlayer prepareToPlay]; [self.moviePlayer play]; }
|
1 2
| self.moviePath = @"http://baobab.wdjcdn.com/1455782903700jy.mp4";
|
2.主角AVPlayer
1 2 3 4
| @property (nonatomic, strong) AVPlayer *player; @property (nonatomic, strong) AVPlayerItem *playerItem; @property (nonatomic, strong) AVPlayerLayer *playerLayer;
|
1 2 3 4 5 6 7
| NSURL *url = [NSURL URLWithString:_playerUrl]; _item = [[AVPlayerItem alloc] initWithURL:url]; _player = [AVPlayer playerWithPlayerItem:_item]; _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player]; _playerLayer.videoGravity = AVLayerVideoGravityResizeAspect; [self.layer addSublayer:_playerLayer]; [_player play];
|
虽然是可以播放视频了但仅仅是能播放而已,暂停播放快进等功能都没有,下面对他进行完善。
1 2 3 4 5 6 7 8 9 10 11 12
| @property (nonatomic, strong) UIView *toolView; @property (nonatomic, strong) UISlider *slider; @property (nonatomic, assign) BOOL isShowToolView; @property (nonatomic, strong) UIButton *playOrPause;
@property (nonatomic, strong) NSTimer *showTimer; @property (nonatomic, strong) NSTimer *progressTimer; @property (nonatomic, strong) UILabel *timerLabel; @property (nonatomic, strong) UILabel *allTimeLabel; @property (nonatomic, strong) UIButton *bigPlayBtn; @property (nonatomic, strong) UIView *coverView;
|
1 2 3 4 5
| self.toolView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - kToolViewHieght, self.bounds.size.width, kToolViewHieght)]; self.toolView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8]; [self addSubview:self.toolView]; [self bringSubviewToFront:self.toolView];
|
1 2 3 4 5 6 7 8 9
| self.playOrPause = [UIButton buttonWithType:UIButtonTypeCustom]; [self.playOrPause setImage:[UIImage imageNamed:@"videoplay"] forState:UIControlStateNormal]; [self.playOrPause setImage:[UIImage imageNamed:@"videopause"] forState:UIControlStateSelected]; self.playOrPause.contentMode = UIViewContentModeCenter; [self.playOrPause addTarget:self action:@selector(playOrPauseClick:) forControlEvents:UIControlEventTouchUpInside]; self.playOrPause.frame = CGRectMake(0, 0, 30, kToolViewHieght); [self.toolView addSubview:self.playOrPause];
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| self.timerLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 40, kToolViewHieght)]; self.timerLabel.font = [UIFont systemFontOfSize:10.0]; self.timerLabel.textColor = [UIColor whiteColor]; [self.toolView addSubview:self.timerLabel];
self.allTimeLabel =[[UILabel alloc]initWithFrame:CGRectMake(70, 0, 40, kToolViewHieght)]; self.allTimeLabel.font = [UIFont systemFontOfSize:10.0]; self.allTimeLabel.textColor = [UIColor whiteColor]; [self.toolView addSubview:self.allTimeLabel];
self.bigPlayBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.bigPlayBtn setImage:[UIImage imageNamed:@"videoplay"] forState:UIControlStateNormal]; [self.bigPlayBtn addTarget:self action:@selector(playOrPauseBigClick:) forControlEvents:UIControlEventTouchUpInside]; self.bigPlayBtn.frame = self.bounds; self.bigPlayBtn.contentMode = UIViewContentModeScaleAspectFill; [self addSubview:self.bigPlayBtn];
self.coverView.hidden = YES; self.toolView.alpha = 0; self.isShowToolView = NO; self.playOrPause.selected = NO;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| - (UISlider *)slider{ if (!_slider) { _slider.backgroundColor = [UIColor whiteColor]; _slider = [[UISlider alloc]initWithFrame:CGRectMake(110, 0, 200, kToolViewHieght)]; [_slider setThumbImage:[UIImage imageNamed:@"videothumb"] forState:UIControlStateNormal]; [_slider addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown]; [_slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; [_slider addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside]; } return _slider; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| - (void)touchDown:(UISlider *)slider{ [self removeProgressTimer]; [self removeShowTime]; } - (void)valueChange:(UISlider *)slider{ NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentItem.duration) * slider.value; self.timerLabel.text = [self timeToStringWithTimeInterval:currentTime]; } - (void)touchUpInside:(UISlider *)slider{ [self addProgressTimer]; NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentItem.duration) * slider.value; [self.player seekToTime:CMTimeMakeWithSeconds(currentTime, NSEC_PER_SEC) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero]; [self toolViewShow]; }
|
1 2 3 4 5 6 7
| - (NSTimer *)progressTimer{ if (!_progressTimer) { _progressTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateProgressInfo) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop]addTimer:self.progressTimer forMode:NSRunLoopCommonModes]; } return _progressTimer; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| - (void)updateProgressInfo{ NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentTime); NSTimeInterval durationTime = CMTimeGetSeconds(self.player.currentItem.duration); self.timerLabel.text = [self timeToStringWithTimeInterval:currentTime]; self.allTimeLabel.text = [self timeToStringWithTimeInterval:durationTime]; self.slider.value = CMTimeGetSeconds(self.player.currentTime)/CMTimeGetSeconds(self.player.currentItem.duration); if (self.slider.value == 1) { [self removeProgressTimer]; self.coverView.hidden = NO; } }
|
1 2 3 4 5
| - (void)removeProgressTimer{ [self.progressTimer invalidate]; self.progressTimer = nil; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| - (void)playTap:(UITapGestureRecognizer *)tap{ if (self.player.status == AVPlayerStatusUnknown) { [self playOrPauseClick:self.playOrPause]; return; } self.isShowToolView = !self.isShowToolView; if (self.isShowToolView) { [UIView animateWithDuration:0.5 animations:^{ self.toolView.alpha = 0.8; }]; if (self.playOrPause.selected) { [self toolViewShow]; } }else{ [self removeShowTime]; [UIView animateWithDuration:0.5 animations:^{ self.toolView.alpha = 0; }]; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| - (void)playOrPauseClick:(UIButton *)btn{ btn.selected = !btn.selected; if (!btn.selected) { self.toolView.alpha = 0.8; [self removeShowTime]; [self.player pause]; [self removeProgressTimer]; }else{ [self toolViewShow]; [self.player play]; [self addProgressTimer]; } }
|
1 2 3 4 5 6 7 8
| - (void)playOrPauseBigClick:(UIButton *)btn{ btn.hidden = YES; self.playOrPause.selected = YES; [self.player replaceCurrentItemWithPlayerItem:self.playerItem]; [self.player play]; [self addProgressTimer]; }
|
1 2 3 4 5 6 7 8 9 10 11
| - (void)toolViewShow{ self.showTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(toolViewHide) userInfo:nil repeats:NO]; [[NSRunLoop mainRunLoop]addTimer:self.showTimer forMode:NSRunLoopCommonModes]; } - (void)toolViewHide{ self.isShowToolView = !self.isShowToolView; [UIView animateWithDuration:0.5 animations:^{ self.toolView.alpha = 0; }]; NSLog(@"工具栏隐藏"); }
|
1 2 3 4 5 6 7 8 9 10
| - (UIView *)coverView{ if (!_coverView) { _coverView = [UIView new]; _coverView.center = self.center; _coverView.bounds = CGRectMake(0, 0, 100, 100); _coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.9]; [self addSubview:_coverView]; } return _coverView; }
|
1 2 3 4 5 6 7
| UIButton *ReplayBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [ReplayBtn setImage:[UIImage imageNamed:@"chongshe"] forState:UIControlStateNormal]; ReplayBtn.center = self.coverView.center; ReplayBtn.frame = CGRectMake(0, 0, 100, 100); [ReplayBtn addTarget:self action:@selector(rePlayClick) forControlEvents:UIControlEventTouchUpInside]; [self.coverView addSubview:ReplayBtn];
|
1 2 3 4 5 6 7
| - (void)rePlayClick{ self.slider.value = 0; [self touchUpInside:self.slider]; self.coverView.hidden = YES; [self playOrPauseBigClick:self.bigPlayBtn]; }
|
- 到这里一个带有播放暂停重播等基本功能的播发器就完成了
后记
- 上面是我用AVPlayer实现播放的一点心得。demo做的很简陋,有诸多的不足,需要完善的地方也有很多,主要是为了给大家提供一个思路抛砖引玉,希望可以帮到大家。
- 惯例放上项目源码还有我的简书,有需要的可以去看一下。
- 参考文章iOS-视频播放器的简单封装