Commit fdc9f3103625849643e834a79bfb9d9eee741605
1 parent
005e66a8
点播播放添加进度条
Showing
1 changed file
with
168 additions
and
10 deletions
LiveVideoCloud/LiveVideoCloud/Classes/Sections/homeSections/Controller/LVCUGCLivePlayerController.m
| @@ -25,6 +25,7 @@ | @@ -25,6 +25,7 @@ | ||
| 25 | LVCUGCPlayerLoadingView *_secondLoadView; | 25 | LVCUGCPlayerLoadingView *_secondLoadView; |
| 26 | LVCUGCPlayerLoadingView *_thirdLoadView; | 26 | LVCUGCPlayerLoadingView *_thirdLoadView; |
| 27 | UIImage *_blurImage; | 27 | UIImage *_blurImage; |
| 28 | + NSTimer *_currentTimer;// 更新当前播放时间 | ||
| 28 | } | 29 | } |
| 29 | 30 | ||
| 30 | @property (nonatomic, strong) UIView *viewOne; | 31 | @property (nonatomic, strong) UIView *viewOne; |
| @@ -34,9 +35,15 @@ | @@ -34,9 +35,15 @@ | ||
| 34 | 35 | ||
| 35 | @property (nonatomic, strong) CNLiveMoviePlayerController *player; // 播放器 | 36 | @property (nonatomic, strong) CNLiveMoviePlayerController *player; // 播放器 |
| 36 | 37 | ||
| 37 | -@property (nonatomic, strong) LVCUGCBackgroundView *bgView; // 背景视图 | 38 | +@property (nonatomic, strong) LVCUGCBackgroundView *bgView; // 背景视图 |
| 38 | 39 | ||
| 39 | @property (nonatomic, strong) UIButton *vodPlayerQuitBtn; | 40 | @property (nonatomic, strong) UIButton *vodPlayerQuitBtn; |
| 41 | +@property (nonatomic, strong) UIView *bottomView; // 下方功能条 | ||
| 42 | +@property (nonatomic, strong) UIButton *playPauseBtn; //播放暂停按钮 | ||
| 43 | +@property (nonatomic, strong) UILabel *currentTimeLabel; //当前时间显示 | ||
| 44 | +@property (nonatomic, strong) UILabel *durationLabel; //总时间 | ||
| 45 | +@property (nonatomic, strong) UISlider *timeSlide; //滑动 | ||
| 46 | +@property (nonatomic, strong) UIProgressView *bufferProgress; //缓冲 | ||
| 40 | 47 | ||
| 41 | @end | 48 | @end |
| 42 | 49 | ||
| @@ -132,6 +139,9 @@ | @@ -132,6 +139,9 @@ | ||
| 132 | 139 | ||
| 133 | } else if ([model.activityStatus isEqualToString:@"2"]) { | 140 | } else if ([model.activityStatus isEqualToString:@"2"]) { |
| 134 | 141 | ||
| 142 | + //用来更新视频当前播放时间 | ||
| 143 | + _currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; | ||
| 144 | + | ||
| 135 | _player = [[CNLiveMoviePlayerController alloc] initVodPlayWithVId:model.activityId | 145 | _player = [[CNLiveMoviePlayerController alloc] initVodPlayWithVId:model.activityId |
| 136 | authSuccess:^{ | 146 | authSuccess:^{ |
| 137 | 147 | ||
| @@ -142,6 +152,7 @@ | @@ -142,6 +152,7 @@ | ||
| 142 | [self.viewSecond addSubview:_player.view]; | 152 | [self.viewSecond addSubview:_player.view]; |
| 143 | [self.viewSecond sendSubviewToBack:_player.view]; | 153 | [self.viewSecond sendSubviewToBack:_player.view]; |
| 144 | [_player.view addGestureRecognizer:_pan]; | 154 | [_player.view addGestureRecognizer:_pan]; |
| 155 | + [_player.view addSubview:self.bottomView]; | ||
| 145 | 156 | ||
| 146 | } authFailed:^(NSDictionary *errorInfo) { | 157 | } authFailed:^(NSDictionary *errorInfo) { |
| 147 | NSLog(@"初始化点播播放器失败%@", errorInfo); | 158 | NSLog(@"初始化点播播放器失败%@", errorInfo); |
| @@ -191,6 +202,7 @@ | @@ -191,6 +202,7 @@ | ||
| 191 | [_player.view removeFromSuperview]; | 202 | [_player.view removeFromSuperview]; |
| 192 | [_player stop]; | 203 | [_player stop]; |
| 193 | _player = nil; | 204 | _player = nil; |
| 205 | + [_currentTimer invalidate]; //时间重置 | ||
| 194 | // 停止后的视图,拿到自己的加载图来显示 | 206 | // 停止后的视图,拿到自己的加载图来显示 |
| 195 | [_viewSecond addSubview:_secondLoadView]; | 207 | [_viewSecond addSubview:_secondLoadView]; |
| 196 | 208 | ||
| @@ -205,9 +217,11 @@ | @@ -205,9 +217,11 @@ | ||
| 205 | if ([_playArray[_index].activityStatus isEqualToString:@"2"]) { | 217 | if ([_playArray[_index].activityStatus isEqualToString:@"2"]) { |
| 206 | 218 | ||
| 207 | [_viewSecond addSubview:self.vodPlayerQuitBtn]; | 219 | [_viewSecond addSubview:self.vodPlayerQuitBtn]; |
| 220 | + _currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; | ||
| 221 | + | ||
| 208 | _player = [[CNLiveMoviePlayerController alloc] initVodPlayWithVId:_playArray[_index].activityId | 222 | _player = [[CNLiveMoviePlayerController alloc] initVodPlayWithVId:_playArray[_index].activityId |
| 209 | authSuccess:^{ | 223 | authSuccess:^{ |
| 210 | - | 224 | + |
| 211 | _player.videoDecoderMode = CNLiveMovieVideoDecoderMode_AUTO; | 225 | _player.videoDecoderMode = CNLiveMovieVideoDecoderMode_AUTO; |
| 212 | _player.view.frame = self.view.bounds; | 226 | _player.view.frame = self.view.bounds; |
| 213 | _player.shouldAutoplay = YES; | 227 | _player.shouldAutoplay = YES; |
| @@ -215,6 +229,7 @@ | @@ -215,6 +229,7 @@ | ||
| 215 | [_viewSecond addSubview:_player.view]; | 229 | [_viewSecond addSubview:_player.view]; |
| 216 | [_viewSecond sendSubviewToBack:_player.view]; | 230 | [_viewSecond sendSubviewToBack:_player.view]; |
| 217 | [_player.view addGestureRecognizer:_pan]; | 231 | [_player.view addGestureRecognizer:_pan]; |
| 232 | + [_player.view addSubview:self.bottomView]; | ||
| 218 | 233 | ||
| 219 | } authFailed:^(NSDictionary *errorInfo) { | 234 | } authFailed:^(NSDictionary *errorInfo) { |
| 220 | NSLog(@"初始化点播播放器失败%@", errorInfo); | 235 | NSLog(@"初始化点播播放器失败%@", errorInfo); |
| @@ -277,6 +292,7 @@ | @@ -277,6 +292,7 @@ | ||
| 277 | } | 292 | } |
| 278 | [_player.view removeFromSuperview]; | 293 | [_player.view removeFromSuperview]; |
| 279 | [_player stop]; | 294 | [_player stop]; |
| 295 | + [_currentTimer invalidate]; //时间重置 | ||
| 280 | _player = nil; | 296 | _player = nil; |
| 281 | // 停止后的视图,拿到自己的加载图来显示 | 297 | // 停止后的视图,拿到自己的加载图来显示 |
| 282 | [_viewSecond addSubview:_secondLoadView]; | 298 | [_viewSecond addSubview:_secondLoadView]; |
| @@ -293,6 +309,8 @@ | @@ -293,6 +309,8 @@ | ||
| 293 | if ([_playArray[_index].activityStatus isEqualToString:@"2"]) { | 309 | if ([_playArray[_index].activityStatus isEqualToString:@"2"]) { |
| 294 | 310 | ||
| 295 | [_viewSecond addSubview:self.vodPlayerQuitBtn]; | 311 | [_viewSecond addSubview:self.vodPlayerQuitBtn]; |
| 312 | + _currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; | ||
| 313 | + | ||
| 296 | _player = [[CNLiveMoviePlayerController alloc] initVodPlayWithVId:_playArray[_index].activityId | 314 | _player = [[CNLiveMoviePlayerController alloc] initVodPlayWithVId:_playArray[_index].activityId |
| 297 | authSuccess:^{ | 315 | authSuccess:^{ |
| 298 | 316 | ||
| @@ -303,6 +321,7 @@ | @@ -303,6 +321,7 @@ | ||
| 303 | [_viewSecond addSubview:_player.view]; | 321 | [_viewSecond addSubview:_player.view]; |
| 304 | [_viewSecond sendSubviewToBack:_player.view]; | 322 | [_viewSecond sendSubviewToBack:_player.view]; |
| 305 | [_player.view addGestureRecognizer:_pan]; | 323 | [_player.view addGestureRecognizer:_pan]; |
| 324 | + [_player.view addSubview:self.bottomView]; | ||
| 306 | 325 | ||
| 307 | } authFailed:^(NSDictionary *errorInfo) { | 326 | } authFailed:^(NSDictionary *errorInfo) { |
| 308 | NSLog(@"初始化点播播放器失败%@", errorInfo); | 327 | NSLog(@"初始化点播播放器失败%@", errorInfo); |
| @@ -497,33 +516,47 @@ | @@ -497,33 +516,47 @@ | ||
| 497 | 516 | ||
| 498 | switch (_player.playbackState) { | 517 | switch (_player.playbackState) { |
| 499 | case MPMoviePlaybackStateStopped: { | 518 | case MPMoviePlaybackStateStopped: { |
| 500 | - | 519 | + if (_playPauseBtn) { |
| 520 | + [self changePlayBtnStatus:NO]; | ||
| 521 | + } | ||
| 501 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateStopped"); | 522 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateStopped"); |
| 502 | break; | 523 | break; |
| 503 | } | 524 | } |
| 504 | case MPMoviePlaybackStatePlaying: { | 525 | case MPMoviePlaybackStatePlaying: { |
| 505 | - | 526 | + if (_playPauseBtn) { |
| 527 | + [self changePlayBtnStatus:YES]; | ||
| 528 | + } | ||
| 506 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStatePlaying"); | 529 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStatePlaying"); |
| 507 | break; | 530 | break; |
| 508 | } | 531 | } |
| 509 | case MPMoviePlaybackStatePaused: | 532 | case MPMoviePlaybackStatePaused: |
| 510 | - | 533 | + if (_playPauseBtn) { |
| 534 | + [self changePlayBtnStatus:NO]; | ||
| 535 | + } | ||
| 511 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStatePaused"); | 536 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStatePaused"); |
| 512 | break; | 537 | break; |
| 513 | case MPMoviePlaybackStateInterrupted: | 538 | case MPMoviePlaybackStateInterrupted: |
| 514 | - | 539 | + if (_playPauseBtn) { |
| 540 | + [self changePlayBtnStatus:NO]; | ||
| 541 | + } | ||
| 515 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateInterrupted"); | 542 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateInterrupted"); |
| 516 | break; | 543 | break; |
| 517 | case MPMoviePlaybackStateSeekingForward: | 544 | case MPMoviePlaybackStateSeekingForward: |
| 518 | - | 545 | + if (_playPauseBtn) { |
| 546 | + [self changePlayBtnStatus:NO]; | ||
| 547 | + } | ||
| 519 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateSeekingForward"); | 548 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateSeekingForward"); |
| 520 | break; | 549 | break; |
| 521 | case MPMoviePlaybackStateSeekingBackward: | 550 | case MPMoviePlaybackStateSeekingBackward: |
| 522 | - | 551 | + if (_playPauseBtn) { |
| 552 | + [self changePlayBtnStatus:NO]; | ||
| 553 | + } | ||
| 523 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateSeekingBackward"); | 554 | NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateSeekingBackward"); |
| 524 | break; | 555 | break; |
| 525 | default: | 556 | default: |
| 526 | - | 557 | + if (_playPauseBtn) { |
| 558 | + [self changePlayBtnStatus:NO]; | ||
| 559 | + } | ||
| 527 | NSLog(@"moviePlayBackStateDidChange : %ld", (long)_player.playbackState); | 560 | NSLog(@"moviePlayBackStateDidChange : %ld", (long)_player.playbackState); |
| 528 | break; | 561 | break; |
| 529 | } | 562 | } |
| @@ -622,7 +655,7 @@ | @@ -622,7 +655,7 @@ | ||
| 622 | - (UIButton *)vodPlayerQuitBtn { | 655 | - (UIButton *)vodPlayerQuitBtn { |
| 623 | if (!_vodPlayerQuitBtn) { | 656 | if (!_vodPlayerQuitBtn) { |
| 624 | _vodPlayerQuitBtn = [UIButton buttonWithType:UIButtonTypeCustom]; | 657 | _vodPlayerQuitBtn = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 625 | - _vodPlayerQuitBtn.frame = CGRectMake(SCREEN_WIDTH - 50, SCREEN_HEIGHT - 50, 40, 40); | 658 | + _vodPlayerQuitBtn.frame = CGRectMake(SCREEN_WIDTH - 50, 50, 40, 40); |
| 626 | [_vodPlayerQuitBtn setImage:[UIImage imageNamed:@"LiveCloseNormal"] forState:UIControlStateNormal]; | 659 | [_vodPlayerQuitBtn setImage:[UIImage imageNamed:@"LiveCloseNormal"] forState:UIControlStateNormal]; |
| 627 | [_vodPlayerQuitBtn setImage:[UIImage imageNamed:@"LiveCloseTouch"] forState:UIControlStateHighlighted]; | 660 | [_vodPlayerQuitBtn setImage:[UIImage imageNamed:@"LiveCloseTouch"] forState:UIControlStateHighlighted]; |
| 628 | [_vodPlayerQuitBtn addTarget:self action:@selector(backToList) forControlEvents:UIControlEventTouchUpInside]; | 661 | [_vodPlayerQuitBtn addTarget:self action:@selector(backToList) forControlEvents:UIControlEventTouchUpInside]; |
| @@ -630,6 +663,131 @@ | @@ -630,6 +663,131 @@ | ||
| 630 | return _vodPlayerQuitBtn; | 663 | return _vodPlayerQuitBtn; |
| 631 | } | 664 | } |
| 632 | 665 | ||
| 666 | +// ************************************** 进度条系列 ************************************** | ||
| 667 | +- (UIView *)bottomView { // 播放器底部功能条 | ||
| 668 | + if (!_bottomView) { | ||
| 669 | + _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)]; | ||
| 670 | + _bottomView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7]; | ||
| 671 | + [_bottomView addSubview:self.playPauseBtn]; | ||
| 672 | + [_bottomView addSubview:self.currentTimeLabel]; | ||
| 673 | + [_bottomView addSubview:self.durationLabel]; | ||
| 674 | + [_bottomView addSubview:self.bufferProgress]; | ||
| 675 | + [_bottomView addSubview:self.timeSlide]; | ||
| 676 | + } | ||
| 677 | + return _bottomView; | ||
| 678 | +} | ||
| 679 | + | ||
| 680 | +- (UIButton *)playPauseBtn { //播放暂停按钮 | ||
| 681 | + if (!_playPauseBtn) { | ||
| 682 | + _playPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom]; | ||
| 683 | + _playPauseBtn.frame = CGRectMake(0, 5, 40, 40); | ||
| 684 | + [_playPauseBtn addTarget:self action:@selector(playPause) forControlEvents:UIControlEventTouchUpInside]; | ||
| 685 | + [_playPauseBtn setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal]; | ||
| 686 | + _playPauseBtn.userInteractionEnabled = NO; | ||
| 687 | + } | ||
| 688 | + return _playPauseBtn; | ||
| 689 | +} | ||
| 690 | + | ||
| 691 | +- (UILabel *)currentTimeLabel { //当前播放时间 | ||
| 692 | + if (!_currentTimeLabel) { | ||
| 693 | + _currentTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 5, 63, 40)]; | ||
| 694 | + _currentTimeLabel.textColor = [UIColor whiteColor]; | ||
| 695 | + _currentTimeLabel.font = [UIFont systemFontOfSize:14]; | ||
| 696 | + [_currentTimeLabel setText:[NSString stringWithFormat:@"00:00:00"]]; | ||
| 697 | + } | ||
| 698 | + return _currentTimeLabel; | ||
| 699 | +} | ||
| 700 | + | ||
| 701 | +- (UILabel *)durationLabel { //总时长 | ||
| 702 | + if (!_durationLabel) { | ||
| 703 | + _durationLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 63, 5, 63, 40)]; | ||
| 704 | + _durationLabel.textColor = [UIColor whiteColor]; | ||
| 705 | + _durationLabel.font = [UIFont systemFontOfSize:14]; | ||
| 706 | + [_durationLabel setText:[NSString stringWithFormat:@"00:00:00"]]; | ||
| 707 | + } | ||
| 708 | + return _durationLabel; | ||
| 709 | +} | ||
| 710 | + | ||
| 711 | +- (UISlider *)timeSlide { // 滑动 | ||
| 712 | + if (!_timeSlide) { | ||
| 713 | + _timeSlide = [[UISlider alloc] initWithFrame:CGRectMake(103, 5, SCREEN_WIDTH - 166, 40)]; | ||
| 714 | + [_timeSlide setMinimumTrackTintColor:[UIColor redColor]]; | ||
| 715 | + [_timeSlide setMaximumTrackTintColor:[UIColor clearColor]]; | ||
| 716 | + [_timeSlide addTarget:self action:@selector(beginScrubbing) forControlEvents:UIControlEventTouchDown]; | ||
| 717 | + [_timeSlide addTarget:self action:@selector(endScrubbing) forControlEvents:UIControlEventTouchUpInside]; | ||
| 718 | + [_timeSlide addTarget:self action:@selector(endScrubbing) forControlEvents:UIControlEventTouchUpOutside]; | ||
| 719 | + } | ||
| 720 | + return _timeSlide; | ||
| 721 | +} | ||
| 722 | + | ||
| 723 | +- (UIProgressView *)bufferProgress { // 缓冲 | ||
| 724 | + if (!_bufferProgress) { | ||
| 725 | + _bufferProgress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; | ||
| 726 | + _bufferProgress.frame = CGRectMake(105, 24, SCREEN_WIDTH - 166-4, 1); | ||
| 727 | + _bufferProgress.trackTintColor = [UIColor whiteColor]; | ||
| 728 | + _bufferProgress.progressTintColor = [UIColor blueColor]; | ||
| 729 | + _bufferProgress.progress = 0; | ||
| 730 | + } | ||
| 731 | + return _bufferProgress; | ||
| 732 | +} | ||
| 733 | + | ||
| 734 | +#pragma mark - 进度条 ACtion !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| 735 | + | ||
| 736 | +- (void)playPause { //播放暂停 | ||
| 737 | + | ||
| 738 | + if (_player.isPlaying) { | ||
| 739 | + [_player pause]; | ||
| 740 | + } else { | ||
| 741 | + [_player play]; | ||
| 742 | + } | ||
| 743 | +} | ||
| 744 | + | ||
| 745 | +- (void)timeChange { // 更新当前播放时间 | ||
| 746 | + | ||
| 747 | + _bufferProgress.progress = _player.playableDuration/_player.duration; | ||
| 748 | + if (!_player.isPreparedToPlay) { | ||
| 749 | + return; | ||
| 750 | + } | ||
| 751 | + _timeSlide.value = _player.currentPlaybackTime; | ||
| 752 | + _timeSlide.maximumValue = _player.duration; | ||
| 753 | + | ||
| 754 | + // 当前播放时间 | ||
| 755 | + NSInteger currentSeconds = (NSInteger)_player.currentPlaybackTime; | ||
| 756 | + NSLog(@"当前时间------> %ld",(long)currentSeconds); | ||
| 757 | + NSInteger seconds = currentSeconds % 60; | ||
| 758 | + NSInteger minutes = currentSeconds / 60; | ||
| 759 | + NSInteger hours = minutes / 60; | ||
| 760 | + | ||
| 761 | + // 总时长 | ||
| 762 | + NSInteger durationall = (NSInteger)_player.duration; | ||
| 763 | + NSInteger durationSeconds = durationall % 60; | ||
| 764 | + NSInteger durationMinutes = durationall / 60; | ||
| 765 | + NSInteger durationHours = durationMinutes / 60; | ||
| 766 | + | ||
| 767 | + // 当前时间 | ||
| 768 | + _currentTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)hours, (long)minutes%60, (long)seconds]; | ||
| 769 | + // 总时长 | ||
| 770 | + _durationLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)durationHours, (long)durationMinutes%60, (long)durationSeconds]; | ||
| 771 | +} | ||
| 772 | + | ||
| 773 | +- (void)beginScrubbing { // 滑动监听方法 | ||
| 774 | + [_player pause]; | ||
| 775 | + [_currentTimer invalidate]; | ||
| 776 | +} | ||
| 777 | +- (void)endScrubbing { | ||
| 778 | + [_player setCurrentPlaybackTime:_timeSlide.value]; | ||
| 779 | + [_player play]; | ||
| 780 | + _currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; | ||
| 781 | +} | ||
| 782 | + | ||
| 783 | +- (void)changePlayBtnStatus:(BOOL)playing { //改变播放暂停按钮图片 | ||
| 784 | + if (playing) { | ||
| 785 | + [_playPauseBtn setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal]; | ||
| 786 | + } else { | ||
| 787 | + [_playPauseBtn setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal]; | ||
| 788 | + } | ||
| 789 | +} | ||
| 790 | + | ||
| 633 | #pragma mark - 移除通知 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | 791 | #pragma mark - 移除通知 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 634 | - (void)removeMovieNotificationObservers | 792 | - (void)removeMovieNotificationObservers |
| 635 | { | 793 | { |