Commit 10a65a4e8a39fc8024b4dc1c89c1f364d3ef7bd8
1 parent
bc4de5f0
卡顿
Showing
3 changed files
with
33 additions
and
21 deletions
CNLivePlayer/Classes/Player/CNLivePlayer.m
| ... | ... | @@ -32,7 +32,8 @@ |
| 32 | 32 | // 1-流畅,2-标清,3-高清,4-超清,默认为2 |
| 33 | 33 | NSString *_rate; |
| 34 | 34 | BOOL _switchRate; |
| 35 | - | |
| 35 | + NSTimeInterval _switchCurrentTime; | |
| 36 | + | |
| 36 | 37 | //音频标识 |
| 37 | 38 | BOOL _isAudio; |
| 38 | 39 | NSString *_roomId; |
| ... | ... | @@ -394,17 +395,17 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 394 | 395 | - (void)createPlayerWithUrl:(NSString *)url { |
| 395 | 396 | self.videoUrl = url; |
| 396 | 397 | if(self->_switchRate){ |
| 398 | + _switchCurrentTime = self.player.currentPlaybackTime; | |
| 399 | + | |
| 397 | 400 | //切换码率 |
| 398 | 401 | NSString *proxyUrl = [[CNLiveHTTPProxyService manager] getProxyUrl:self.videoUrl]; |
| 399 | 402 | KSYMoviePlayerController *switchPlayer = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:PLAYER_OpenCache?proxyUrl:self.videoUrl]]; |
| 400 | -// switchPlayer.view.frame = self.view.bounds; | |
| 401 | - switchPlayer.view.frame = CGRectMake(100, 200, 200, 100); | |
| 403 | + switchPlayer.view.frame = self.view.bounds; | |
| 404 | +// switchPlayer.view.frame = CGRectMake(0, 200, self.view.bounds.size.width, 200); | |
| 402 | 405 | switchPlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| 403 | - switchPlayer.shouldAutoplay = NO; | |
| 404 | 406 | switchPlayer.shouldEnableKSYStatModule = NO; |
| 407 | + switchPlayer.shouldMute = YES; | |
| 405 | 408 | [switchPlayer prepareToPlay]; |
| 406 | - [switchPlayer play]; | |
| 407 | - [switchPlayer seekTo:self.player.currentPlaybackTime accurate:YES]; | |
| 408 | 409 | [self.view addSubview:switchPlayer.view]; |
| 409 | 410 | self.switchPlayer = switchPlayer; |
| 410 | 411 | |
| ... | ... | @@ -709,33 +710,43 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 709 | 710 | //切换码率 |
| 710 | 711 | if(_switchRate){ |
| 711 | 712 | if((self.switchPlayer == player)&&(loadState == 3)){ |
| 712 | - [player seekTo:self.player.currentPlaybackTime accurate:NO]; | |
| 713 | - [player play]; | |
| 714 | - [self.player stop]; | |
| 713 | + | |
| 715 | 714 | KSYMoviePlayerController *temp = self.player; |
| 715 | + temp.shouldMute = NO; | |
| 716 | 716 | self.player = player; |
| 717 | 717 | self.switchPlayer = temp; |
| 718 | -// __weak typeof(self)weakSelf = self; | |
| 719 | -// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | |
| 720 | -// __strong typeof (weakSelf) strongSelf = weakSelf; | |
| 721 | -// if(!strongSelf){ | |
| 722 | -// return ; | |
| 723 | -// } | |
| 724 | -// [strongSelf.switchPlayer stop]; | |
| 725 | -// }); | |
| 718 | + | |
| 719 | + __weak typeof(self)weakSelf = self; | |
| 720 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | |
| 721 | + __strong typeof (weakSelf) strongSelf = weakSelf; | |
| 722 | + if(!strongSelf){ | |
| 723 | + return ; | |
| 724 | + } | |
| 725 | + strongSelf.player.shouldMute = NO; | |
| 726 | + [strongSelf.switchPlayer reset:NO]; | |
| 727 | + [strongSelf.switchPlayer stop]; | |
| 728 | + [strongSelf.switchPlayer.view removeFromSuperview]; | |
| 729 | + | |
| 730 | + }); | |
| 731 | + | |
| 726 | 732 | _switchRate = NO; |
| 733 | + | |
| 727 | 734 | } |
| 728 | 735 | } |
| 729 | - [_player play]; | |
| 736 | + [player play]; | |
| 730 | 737 | |
| 731 | 738 | } |
| 732 | 739 | |
| 733 | 740 | - (void)mediaIsPreparedToPlayDidChange:(NSNotification *)notification { |
| 734 | 741 | KSYMoviePlayerController *player = [notification object]; |
| 735 | - MPMovieLoadState loadState = player.loadState; | |
| 742 | + if(self.switchPlayer == player){ | |
| 743 | +// [player seekTo:_switchCurrentTime accurate:YES]; | |
| 744 | + player.currentPlaybackTime = _switchCurrentTime+5.0; | |
| 745 | + [player play]; | |
| 736 | 746 | |
| 747 | + } | |
| 737 | 748 | if (PLAYER_TestEnvironment) { |
| 738 | - NSLog(@"CNLivePlayer-->播放器缓冲至可播放状态"); | |
| 749 | + NSLog(@"CNLivePlayer-->播放器开始预播放"); | |
| 739 | 750 | } |
| 740 | 751 | |
| 741 | 752 | if (!_contentType || _contentType.length == 0) { | ... | ... |
Example/CNLivePlayer/Player/CNLivePlayerController.m
| ... | ... | @@ -59,7 +59,7 @@ |
| 59 | 59 | // |
| 60 | 60 | // }]; |
| 61 | 61 | |
| 62 | - UILabel *state = [[UILabel alloc]initWithFrame:CGRectMake(100, 400, 200, 20)]; | |
| 62 | + UILabel *state = [[UILabel alloc]initWithFrame:CGRectMake(100, 300, 200, 20)]; | |
| 63 | 63 | state.backgroundColor = [UIColor redColor]; |
| 64 | 64 | state.text = @"切换中"; |
| 65 | 65 | [self.view addSubview:state]; | ... | ... |