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,7 +32,8 @@ | ||
| 32 | // 1-流畅,2-标清,3-高清,4-超清,默认为2 | 32 | // 1-流畅,2-标清,3-高清,4-超清,默认为2 |
| 33 | NSString *_rate; | 33 | NSString *_rate; |
| 34 | BOOL _switchRate; | 34 | BOOL _switchRate; |
| 35 | - | 35 | + NSTimeInterval _switchCurrentTime; |
| 36 | + | ||
| 36 | //音频标识 | 37 | //音频标识 |
| 37 | BOOL _isAudio; | 38 | BOOL _isAudio; |
| 38 | NSString *_roomId; | 39 | NSString *_roomId; |
| @@ -394,17 +395,17 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang | @@ -394,17 +395,17 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang | ||
| 394 | - (void)createPlayerWithUrl:(NSString *)url { | 395 | - (void)createPlayerWithUrl:(NSString *)url { |
| 395 | self.videoUrl = url; | 396 | self.videoUrl = url; |
| 396 | if(self->_switchRate){ | 397 | if(self->_switchRate){ |
| 398 | + _switchCurrentTime = self.player.currentPlaybackTime; | ||
| 399 | + | ||
| 397 | //切换码率 | 400 | //切换码率 |
| 398 | NSString *proxyUrl = [[CNLiveHTTPProxyService manager] getProxyUrl:self.videoUrl]; | 401 | NSString *proxyUrl = [[CNLiveHTTPProxyService manager] getProxyUrl:self.videoUrl]; |
| 399 | KSYMoviePlayerController *switchPlayer = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:PLAYER_OpenCache?proxyUrl:self.videoUrl]]; | 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 | switchPlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; | 405 | switchPlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| 403 | - switchPlayer.shouldAutoplay = NO; | ||
| 404 | switchPlayer.shouldEnableKSYStatModule = NO; | 406 | switchPlayer.shouldEnableKSYStatModule = NO; |
| 407 | + switchPlayer.shouldMute = YES; | ||
| 405 | [switchPlayer prepareToPlay]; | 408 | [switchPlayer prepareToPlay]; |
| 406 | - [switchPlayer play]; | ||
| 407 | - [switchPlayer seekTo:self.player.currentPlaybackTime accurate:YES]; | ||
| 408 | [self.view addSubview:switchPlayer.view]; | 409 | [self.view addSubview:switchPlayer.view]; |
| 409 | self.switchPlayer = switchPlayer; | 410 | self.switchPlayer = switchPlayer; |
| 410 | 411 | ||
| @@ -709,33 +710,43 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang | @@ -709,33 +710,43 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang | ||
| 709 | //切换码率 | 710 | //切换码率 |
| 710 | if(_switchRate){ | 711 | if(_switchRate){ |
| 711 | if((self.switchPlayer == player)&&(loadState == 3)){ | 712 | if((self.switchPlayer == player)&&(loadState == 3)){ |
| 712 | - [player seekTo:self.player.currentPlaybackTime accurate:NO]; | ||
| 713 | - [player play]; | ||
| 714 | - [self.player stop]; | 713 | + |
| 715 | KSYMoviePlayerController *temp = self.player; | 714 | KSYMoviePlayerController *temp = self.player; |
| 715 | + temp.shouldMute = NO; | ||
| 716 | self.player = player; | 716 | self.player = player; |
| 717 | self.switchPlayer = temp; | 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 | _switchRate = NO; | 732 | _switchRate = NO; |
| 733 | + | ||
| 727 | } | 734 | } |
| 728 | } | 735 | } |
| 729 | - [_player play]; | 736 | + [player play]; |
| 730 | 737 | ||
| 731 | } | 738 | } |
| 732 | 739 | ||
| 733 | - (void)mediaIsPreparedToPlayDidChange:(NSNotification *)notification { | 740 | - (void)mediaIsPreparedToPlayDidChange:(NSNotification *)notification { |
| 734 | KSYMoviePlayerController *player = [notification object]; | 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 | if (PLAYER_TestEnvironment) { | 748 | if (PLAYER_TestEnvironment) { |
| 738 | - NSLog(@"CNLivePlayer-->播放器缓冲至可播放状态"); | 749 | + NSLog(@"CNLivePlayer-->播放器开始预播放"); |
| 739 | } | 750 | } |
| 740 | 751 | ||
| 741 | if (!_contentType || _contentType.length == 0) { | 752 | if (!_contentType || _contentType.length == 0) { |
Example/CNLivePlayer/Player/CNLivePlayerController.m
| @@ -59,7 +59,7 @@ | @@ -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 | state.backgroundColor = [UIColor redColor]; | 63 | state.backgroundColor = [UIColor redColor]; |
| 64 | state.text = @"切换中"; | 64 | state.text = @"切换中"; |
| 65 | [self.view addSubview:state]; | 65 | [self.view addSubview:state]; |
Example/Podfile
| @@ -7,6 +7,7 @@ platform :ios, '9.0' | @@ -7,6 +7,7 @@ platform :ios, '9.0' | ||
| 7 | target 'CNLivePlayer_Example' do | 7 | target 'CNLivePlayer_Example' do |
| 8 | pod 'CNLivePlayer', :path => '../' | 8 | pod 'CNLivePlayer', :path => '../' |
| 9 | pod 'YYImage' | 9 | pod 'YYImage' |
| 10 | +# pod 'SJVideoPlayer' | ||
| 10 | 11 | ||
| 11 | target 'CNLivePlayer_Tests' do | 12 | target 'CNLivePlayer_Tests' do |
| 12 | inherit! :search_paths | 13 | inherit! :search_paths |