Commit e67a39d9e89585d6d8de9e7759662f7b840d1d9c

Authored by zhangxiaowen
1 parent a94c9c99

no message

CNLivePlayer/Classes/Player/CNLivePlayer.h
@@ -167,6 +167,14 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo); @@ -167,6 +167,14 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo);
167 */ 167 */
168 - (void)setPlayerWithContentId:(NSString *)contentId type:(CNLivePlayerType)type authSuccess:(AuthSuccessBlock)authSuccessBlock authFailure:(AuthFailureBlock)authFailureBlock; 168 - (void)setPlayerWithContentId:(NSString *)contentId type:(CNLivePlayerType)type authSuccess:(AuthSuccessBlock)authSuccessBlock authFailure:(AuthFailureBlock)authFailureBlock;
169 169
  170 +/* 给播放器切换码率设置内容id 设置成功后才能播放(如:自动播放、是否开启硬件解码等)和调用准备播放视频方法(prepareToPlay)
  171 + * 切换视频
  172 + * rate 码率 1-流畅,2-标清,3-高清,4-超清,默认为2
  173 + * type 内容类型
  174 + *
  175 + */
  176 +- (void)setPlayerWithRate:(NSString *)rate authSuccess:(AuthSuccessBlock)authSuccessBlock authFailure:(AuthFailureBlock)authFailureBlock;
  177 +
170 /* 178 /*
171 * 销毁播放器 179 * 销毁播放器
172 */ 180 */
CNLivePlayer/Classes/Player/CNLivePlayer.m
@@ -28,6 +28,9 @@ @@ -28,6 +28,9 @@
28 //直播 28 //直播
29 NSString *_activityId; 29 NSString *_activityId;
30 30
  31 + // 1-流畅,2-标清,3-高清,4-超清,默认为2
  32 + NSString *_rate;
  33 +
31 //音频标识 34 //音频标识
32 BOOL _isAudio; 35 BOOL _isAudio;
33 NSString *_roomId; 36 NSString *_roomId;
@@ -70,11 +73,12 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -70,11 +73,12 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
70 _contentType = @""; 73 _contentType = @"";
71 _contentId = @""; 74 _contentId = @"";
72 _idName = @""; 75 _idName = @"";
73 - 76 + _rate = @"2";
  77 +
74 [self addMovieNotificationObservers]; 78 [self addMovieNotificationObservers];
75 79
76 self.player = [[KSYMoviePlayerController alloc] initWithContentURL:url]; 80 self.player = [[KSYMoviePlayerController alloc] initWithContentURL:url];
77 - [self.view addSubview:_player.view]; 81 + [self.view addSubview:self.player.view];
78 _player.view.frame = self.view.bounds; 82 _player.view.frame = self.view.bounds;
79 _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 83 _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
80 _player.shouldEnableKSYStatModule = NO; 84 _player.shouldEnableKSYStatModule = NO;
@@ -140,6 +144,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -140,6 +144,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
140 _contentId = channelId; 144 _contentId = channelId;
141 _activityId = [NSString stringWithFormat:@"%@",activityId]; 145 _activityId = [NSString stringWithFormat:@"%@",activityId];
142 _isAudio = NO; 146 _isAudio = NO;
  147 +
143 if (agree) { 148 if (agree) {
144 //初始化IM 149 //初始化IM
145 [[CNLiveMsgManager sharedCNLiveMsgManager] initCNLiveMsgToolsWithAppKey:PLAYER_AppKey appId:PLAYER_AppID]; 150 [[CNLiveMsgManager sharedCNLiveMsgManager] initCNLiveMsgToolsWithAppKey:PLAYER_AppKey appId:PLAYER_AppID];
@@ -189,6 +194,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -189,6 +194,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
189 - (void)createPlayerWithContentId:(NSString *)contentId authSuccess:(AuthSuccessBlock)authSuccessBlock authFailure:(AuthFailureBlock)authFailureBlock{ 194 - (void)createPlayerWithContentId:(NSString *)contentId authSuccess:(AuthSuccessBlock)authSuccessBlock authFailure:(AuthFailureBlock)authFailureBlock{
190 [UIApplication sharedApplication].idleTimerDisabled = YES; 195 [UIApplication sharedApplication].idleTimerDisabled = YES;
191 196
  197 + _rate = @"2";
192 _retryTime = 0; 198 _retryTime = 0;
193 [self addMovieNotificationObservers]; 199 [self addMovieNotificationObservers];
194 200
@@ -211,6 +217,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -211,6 +217,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
211 if([_contentId isEqualToString:contentId]){ 217 if([_contentId isEqualToString:contentId]){
212 return; 218 return;
213 } 219 }
  220 + [self pause];
214 _retryTime = 0; 221 _retryTime = 0;
215 _type = type; 222 _type = type;
216 switch (type) { 223 switch (type) {
@@ -251,6 +258,22 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -251,6 +258,22 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
251 258
252 } 259 }
253 260
  261 +/* 给播放器切换码率设置内容id 设置成功后才能播放(如:自动播放、是否开启硬件解码等)和调用准备播放视频方法(prepareToPlay)
  262 + * 切换视频
  263 + * rate 码率 1-流畅,2-标清,3-高清,4-超清,默认为2
  264 + * type 内容类型
  265 + *
  266 + */
  267 +- (void)setPlayerWithRate:(NSString *)rate authSuccess:(AuthSuccessBlock)authSuccessBlock authFailure:(AuthFailureBlock)authFailureBlock{
  268 + _rate = rate;
  269 + __weak typeof(self) weakSelf = self;
  270 + dispatch_async(dispatch_get_global_queue(0, 0), ^{
  271 + __strong typeof(self) strongSelf = weakSelf;
  272 + [strongSelf getUrlSuccess:authSuccessBlock failure:authFailureBlock];
  273 +
  274 + });
  275 +}
  276 +
254 /* 277 /*
255 * 销毁播放器 278 * 销毁播放器
256 */ 279 */
@@ -280,7 +303,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -280,7 +303,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
280 303
281 #pragma mark - 查找本地是否有缓存url 304 #pragma mark - 查找本地是否有缓存url
282 - (void)getUrlSuccess:(void (^)(void))authSuccessBlock failure:(void (^)(NSDictionary *errorDic))authFailureBlock { 305 - (void)getUrlSuccess:(void (^)(void))authSuccessBlock failure:(void (^)(NSDictionary *errorDic))authFailureBlock {
283 - id data = [[CNLivePlayerDBTool shared] searchInTable:_contentId]; 306 + NSString *key = [NSString stringWithFormat:@"%@:%@",_contentId,_rate];
  307 + id data = [[CNLivePlayerDBTool shared] searchInTable:key];
284 CNLivePlayerDBModel *model = (CNLivePlayerDBModel *)data; 308 CNLivePlayerDBModel *model = (CNLivePlayerDBModel *)data;
285 //1.判断是否存在缓存url 309 //1.判断是否存在缓存url
286 if (![model isEmpty]) { 310 if (![model isEmpty]) {
@@ -404,6 +428,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -404,6 +428,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
404 @"playType": _isAudio ? @"a": @"v", 428 @"playType": _isAudio ? @"a": @"v",
405 @"platform_id": [NSBundle mainBundle].bundleIdentifier, 429 @"platform_id": [NSBundle mainBundle].bundleIdentifier,
406 @"channelName": PLAYER_ChannelName, 430 @"channelName": PLAYER_ChannelName,
  431 + @"rate":_rate?_rate:@"2",
407 @"tag": PLAYER_Tag, 432 @"tag": PLAYER_Tag,
408 @"from": @"apple", 433 @"from": @"apple",
409 @"sid": PLAYER_UserId, 434 @"sid": PLAYER_UserId,
@@ -414,7 +439,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -414,7 +439,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
414 __weak typeof(self) weakSelf = self; 439 __weak typeof(self) weakSelf = self;
415 [[CNLivePlayerNetworkManager manager] getVideoURL:videoUrl success:^(NSString * _Nullable videoURL, NSInteger code) { 440 [[CNLivePlayerNetworkManager manager] getVideoURL:videoUrl success:^(NSString * _Nullable videoURL, NSInteger code) {
416 __strong typeof(self) strongSelf = weakSelf; 441 __strong typeof(self) strongSelf = weakSelf;
417 - CNLivePlayerDBModel *model = [[CNLivePlayerDBModel alloc]initWithContenId:self->_contentId url:videoURL]; 442 + NSString *key = [NSString stringWithFormat:@"%@:%@",strongSelf->_contentId, strongSelf->_rate];
  443 + CNLivePlayerDBModel *model = [[CNLivePlayerDBModel alloc]initWithContenId:key url:videoURL];
418 [[CNLivePlayerDBTool shared] insertTable:model]; 444 [[CNLivePlayerDBTool shared] insertTable:model];
419 445
420 __weak typeof(self) weakSelf = strongSelf; 446 __weak typeof(self) weakSelf = strongSelf;
@@ -490,8 +516,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -490,8 +516,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
490 url = videoUrl; 516 url = videoUrl;
491 517
492 } 518 }
493 -  
494 - CNLivePlayerDBModel *model = [[CNLivePlayerDBModel alloc]initWithContenId:_contentId url:url]; 519 + NSString *key = [NSString stringWithFormat:@"%@:%@",strongSelf->_contentId, strongSelf->_rate];
  520 + CNLivePlayerDBModel *model = [[CNLivePlayerDBModel alloc]initWithContenId:key url:url];
495 [[CNLivePlayerDBTool shared] insertTable:model]; 521 [[CNLivePlayerDBTool shared] insertTable:model];
496 522
497 __weak typeof(self) weakSelf = strongSelf; 523 __weak typeof(self) weakSelf = strongSelf;
@@ -712,8 +738,11 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -712,8 +738,11 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
712 NSDictionary *userInfo = notification.userInfo; 738 NSDictionary *userInfo = notification.userInfo;
713 float progress = [userInfo[CacheProgressKey] floatValue]; 739 float progress = [userInfo[CacheProgressKey] floatValue];
714 if ((NSInteger)(progress * 10000) > 10000) { 740 if ((NSInteger)(progress * 10000) > 10000) {
  741 + __weak typeof(self) weakSelf = self;
715 dispatch_async(dispatch_get_main_queue(), ^{ 742 dispatch_async(dispatch_get_main_queue(), ^{
716 - [[CNLivePlayerDBTool shared] updateInTable:_contentId]; 743 + __strong typeof(self) strongSelf = weakSelf;
  744 + NSString *key = [NSString stringWithFormat:@"%@:%@",strongSelf->_contentId, strongSelf->_rate];
  745 + [[CNLivePlayerDBTool shared] updateInTable:key];
717 }); 746 });
718 } 747 }
719 } 748 }
@@ -869,6 +898,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang @@ -869,6 +898,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang
869 * 视频暂停 898 * 视频暂停
870 */ 899 */
871 - (void)pause{ 900 - (void)pause{
  901 + [_player reset:YES];
872 [_player pause]; 902 [_player pause];
873 903
874 } 904 }
CNLivePlayer/Classes/Player/CNLivePlayerTools.m
@@ -41,7 +41,7 @@ @@ -41,7 +41,7 @@
41 41
42 } 42 }
43 43
44 -+ (NSString *)signvalue:(NSDictionary*)parameter { 44 ++ (NSString *)signvalue:(NSDictionary *)parameter {
45 //对所有传入参数按照字段名的 ASCII 码从小到大排序 45 //对所有传入参数按照字段名的 ASCII 码从小到大排序
46 NSArray *keyArr=[parameter allKeys]; 46 NSArray *keyArr=[parameter allKeys];
47 NSArray *arr = [keyArr sortedArrayUsingSelector:@selector(compare:)]; 47 NSArray *arr = [keyArr sortedArrayUsingSelector:@selector(compare:)];
Example/CNLivePlayer/CNLivePlayerController.m
@@ -22,35 +22,33 @@ @@ -22,35 +22,33 @@
22 self.view.backgroundColor = [UIColor whiteColor]; 22 self.view.backgroundColor = [UIColor whiteColor];
23 23
24 // Do any additional setup after loading the view. 24 // Do any additional setup after loading the view.
25 - // _player = [[CNLivePlayer alloc]initVodPlayerWithVideoId:@"82_c8ff347048414b7295cef882f8361073" authSuccess:^{  
26 - // NSLog(@"---------------初始化播放器成功---------------");  
27 - // _player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);  
28 - // [_player prepareToPlay];  
29 - // [self.view addSubview:_player.view];  
30 - // } authFailed:^(NSDictionary * _Nonnull errorInfo) {  
31 - // NSLog(@"%@", errorInfo);  
32 - //  
33 - // }];  
34 25
35 - //1066_716fabeaafb64236b60ecf74167c5c96  
36 - //82_c8ff347048414b7295cef882f8361073  
37 - __weak typeof(self) weakSelf = self;  
38 - self.player = [[CNLivePlayer alloc]initAudioPlayerWithAudioId:@"1066_716fabeaafb64236b60ecf74167c5c96" authSuccess:^{  
39 - NSLog(@"---------------初始化播放器成功---------------");  
40 - __strong typeof(self) strongSelf = weakSelf;  
41 - strongSelf.player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);  
42 -  
43 - [strongSelf.player prepareToPlay];  
44 - [strongSelf.view addSubview:weakSelf.player.view];  
45 -  
46 - NSLog(@"-----%@", strongSelf.player.contentURL);  
47 -  
48 - 26 + _player = [[CNLivePlayer alloc]initVodPlayerWithVideoId:@"82_c8ff347048414b7295cef882f8361073" authSuccess:^{
  27 + _player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);
  28 + [_player prepareToPlay];
  29 + [self.view addSubview:_player.view];
49 } authFailure:^(NSDictionary * _Nonnull errorInfo) { 30 } authFailure:^(NSDictionary * _Nonnull errorInfo) {
50 - NSLog(@"%@", errorInfo);  
51 - 31 +
52 }]; 32 }];
53 - _player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0); 33 +
  34 + //1066_716fabeaafb64236b60ecf74167c5c96
  35 + //82_c8ff347048414b7295cef882f8361073
  36 +// __weak typeof(self) weakSelf = self;
  37 +// self.player = [[CNLivePlayer alloc]initAudioPlayerWithAudioId:@"1066_716fabeaafb64236b60ecf74167c5c96" authSuccess:^{
  38 +// NSLog(@"---------------初始化播放器成功---------------");
  39 +// __strong typeof(self) strongSelf = weakSelf;
  40 +// strongSelf.player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);
  41 +//
  42 +// [strongSelf.player prepareToPlay];
  43 +// [strongSelf.view addSubview:weakSelf.player.view];
  44 +//
  45 +// NSLog(@"-----%@", strongSelf.player.contentURL);
  46 +//
  47 +//
  48 +// } authFailure:^(NSDictionary * _Nonnull errorInfo) {
  49 +// NSLog(@"%@", errorInfo);
  50 +//
  51 +// }];
54 52
55 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 53 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
56 btn.frame =CGRectMake(0, 0, 100, 100); 54 btn.frame =CGRectMake(0, 0, 100, 100);
@@ -86,12 +84,21 @@ @@ -86,12 +84,21 @@
86 } 84 }
87 85
88 - (void)btnDidClicked{ 86 - (void)btnDidClicked{
89 - [_player setPlayerWithContentId:@"82_c8ff347048414b7295cef882f8361073" type:CNLivePlayerVod authSuccess:^{  
90 - [_player prepareToPlay]; 87 +
  88 + __weak typeof(self) weakSelf = self;
  89 + [_player setPlayerWithContentId:@"1066_716fabeaafb64236b60ecf74167c5c96" type:CNLivePlayerVod authSuccess:^{
  90 + __strong typeof(self) strongSelf = weakSelf;
  91 + [strongSelf.player prepareToPlay];
91 92
92 } authFailure:^(NSDictionary * _Nonnull errorInfo) { 93 } authFailure:^(NSDictionary * _Nonnull errorInfo) {
93 94
94 }]; 95 }];
  96 +// [_player setPlayerWithRate:@"4" authSuccess:^{
  97 +// __strong typeof(self) strongSelf = weakSelf;
  98 +// [strongSelf.player prepareToPlay];
  99 +// } authFailure:^(NSDictionary * _Nonnull errorInfo) {
  100 +//
  101 +// }];
95 102
96 } 103 }
97 - (void)backDidClicked{ 104 - (void)backDidClicked{