LVCLivePlayerController.m
13.9 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//
// LVCLivePlayerController.m
// LiveVideoCloud
//
// Created by iOS on 17/3/13.
// Copyright © 2017年 cnlive. All rights reserved.
//
#import "LVCLivePlayerController.h"
#import "LVCPlayer.h"
#import "LVCFullScreenPlayerController.h"
#import "LVCInputView.h"
#import "LVCChatView.h"
#import "CNBarrageManager.h"
#import "CNBarrageView.h"
#import "chatMsgModel.h"
@interface LVCLivePlayerController ()<LVCPlayerDelegate,LVCInputViewDelegate,LVCChatViewDelegate,UIGestureRecognizerDelegate>
{
BOOL isHiddenStatusBar; //记录状态的隐藏显示
CGRect playerFrame; //播放器原始位置
NSInteger _originalDirection; //记录原始方向
CGFloat keyboardHeight; //键盘高度
NSString *_roomId; //聊天室ID
}
@property (nonatomic, strong) LVCPlayer *player;
@property (nonatomic, strong) LVCFullScreenPlayerController *fullVC;
@property (nonatomic, strong) LVCInputView *inputView;
@property (nonatomic, strong) LVCChatView *chatView;
@property (nonatomic, strong) CNBarrageManager *barrageManager; //弹幕管理者
@property (nonatomic ,strong) UIView *barrageBaseView; //弹幕总View
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@end
@implementation LVCLivePlayerController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.hidden = YES;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
//直播时长统计开始
[CNLiveStat beginEvent:@"PGCLiveEvent"];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
self.navigationController.navigationBar.hidden = NO;
//直播时长统计结束
[CNLiveStat endEvent:@"PGCLiveEvent"];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden: NO withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
//直播播放数量统计
[CNLiveStat statLivePlayerWithChannelId:@"iosLivePlayStatTest"];
_roomId = [NSString stringWithFormat:@"syn%@",self.model.mediaId];
playerFrame = CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_WIDTH*9/16);
_originalDirection = 0;
//注册通知
[self addObservers];
[self.view addSubview:self.player];
[self.view addSubview:self.chatView];
[self.view addSubview:self.inputView];
//隐藏和显示输入框
__weak typeof(self) weakSelf = self;
self.player.hideBlock = ^(BOOL hide) {
weakSelf.inputView.hidden = hide;
if (!hide) {
[weakSelf.inputView.inputTextField becomeFirstResponder];
}
};
[self makeConstraints];
[_player autoHideControlView]; //自动隐藏工具条
self.barrageManager.generateBarrageBlock = ^(CNBarrageView *barrageView) {
[weakSelf addBarrageView:barrageView];
};
[self.player.contentView addSubview:self.barrageBaseView];
_tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
_tap.delegate = self;
[self.view addGestureRecognizer:self.tap];
}
- (void)addBarrageView:(CNBarrageView *)barrageView {
barrageView.frame = CGRectMake(self.player.width+50, barrageView.trajectory, barrageView.width, barrageView.height);
[self.barrageBaseView addSubview:barrageView];
[barrageView startAnimation];
}
- (void)addObservers
{
//设备旋转
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onDeviceOrientationChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark -
#pragma mark - Notification
- (void)onDeviceOrientationChange:(NSNotification *)notification
{
if (_player == nil|| _player.superview == nil) return;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case UIDeviceOrientationPortrait:
{
if (_originalDirection == 1) {
[self toHalfScreenWithrotationAngle:M_PI_2];
} else if (_originalDirection == 2){
[self toHalfScreenWithrotationAngle:-M_PI_2];
}
_originalDirection = 0;
}
break;
case UIDeviceOrientationLandscapeLeft:
{
if (_originalDirection == 0 ) {
[self toFullWithrotationAngle:-M_PI_2];
}
_originalDirection = 1;
}
break;
case UIDeviceOrientationLandscapeRight:
{
if (_originalDirection == 0 ) {
[self toFullWithrotationAngle:M_PI_2];
}
_originalDirection = 2;
}
break;
default:
break;
}
}
//当键盘出现
- (void)keyboardWillShow:(NSNotification *)aNotification{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
_inputView.frame = CGRectMake(0, SCREEN_HEIGHT-49-height, SCREEN_WIDTH, 49);
}
- (void)keyboardWillHide:(NSNotification *)aNotification{
_inputView.frame = CGRectMake(0, SCREEN_HEIGHT-49, SCREEN_WIDTH, 49);
}
#pragma mark -
#pragma mark - 屏幕旋转
//旋转至全屏
- (void)toFullWithrotationAngle:(CGFloat)angle
{
_inputView.hidden = YES;
if (_player.isFullScreen) return;
_player.isFullScreen = YES;
[self presentViewController:self.fullVC animated:NO completion:^{
[_player removeFromSuperview];
[self.fullVC.view addSubview:self.player];
[_inputView removeFromSuperview];
[self.fullVC.view addSubview:self.inputView];
[self makePlayerViewConstraints:YES];
self.player.transform = CGAffineTransformMakeRotation(angle);
[UIView animateWithDuration:0.3 animations:^{
self.player.transform = CGAffineTransformMakeRotation(0);
}];
//是否显示弹幕
if ([_player displayBarrage]) {
[self.player.contentView addSubview:self.barrageBaseView];
[self.player.contentView insertSubview:self.barrageBaseView atIndex:1];
self.barrageManager.isStopBarrage = NO;
} else {
self.barrageManager.isStopBarrage = YES;
[self.barrageManager stopBarrage];
[self.barrageBaseView removeFromSuperview];
}
}];
}
//旋转至半屏
- (void)toHalfScreenWithrotationAngle:(CGFloat)angle
{
_player.isFullScreen = NO;
_inputView.hidden = NO;
[_fullVC dismissViewControllerAnimated:NO completion:^{
[_player removeFromSuperview];
[self.view addSubview:self.player];
[self.inputView removeFromSuperview];
[self.view addSubview:self.inputView];
[self makePlayerViewConstraints:NO];
self.player.transform = CGAffineTransformMakeRotation(angle);
[UIView animateWithDuration:0.3 animations:^{
self.player.transform = CGAffineTransformMakeRotation(0);
}];
self.barrageManager.isStopBarrage = YES;
[self.barrageManager stopBarrage];
[self.barrageBaseView removeFromSuperview];
}];
}
#pragma mark -
#pragma mark - LVCChatViewDelegate
- (void)lvcChatView:(LVCChatView *)chatView sendBarrageWithModel:(chatMsgModel *)model
{
if (_player.isFullScreen) {
[self.barrageManager startBarrageWithNewArray:[NSMutableArray arrayWithObject:model]];
}
}
#pragma mark -
#pragma mark - LVCPlayerDelegate
- (void)lvcPlayer:(LVCPlayer *)player clickedBackBtn:(UIButton *)backBtn
{
//如果是全屏返回半屏,如果是半屏直接退出
if (_player.isFullScreen) {
[self toHalfScreenWithrotationAngle:M_PI_2];
self.player.isFullScreen = NO;
} else {
[_player stopPlaying];
//退出聊天室
[_chatView quitChatroom];
[self removeViews];
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)lvcPlayer:(LVCPlayer *)player clickedFullScreenBtn:(UIButton *)fullScreenBtn
{
if (_player.isFullScreen) {//全屏
[self toHalfScreenWithrotationAngle:M_PI_2];
self.player.isFullScreen = NO;
}else{//非全屏
[self toFullWithrotationAngle:-M_PI_2];
}
}
- (void)lvcPlayer:(LVCPlayer *)player clickedEditBtn:(UIButton *)editBtn
{
}
- (void)lvcPlayer:(LVCPlayer *)player barrageSwitch:(BOOL)on
{
if (on) { //开启弹幕
[self.player.contentView addSubview:self.barrageBaseView];
[self.player.contentView insertSubview:self.barrageBaseView atIndex:1];
self.barrageManager.isStopBarrage = NO;
} else { //关闭弹幕
self.barrageManager.isStopBarrage = YES;
[self.barrageManager stopBarrage];
[self.barrageBaseView removeFromSuperview];
}
}
#pragma mark -
#pragma mark - LVCInputViewDelegate
- (void)lvcInputView:(LVCInputView *)inputView sendMessage:(NSString *)message
{
[_chatView sendMsgWithMessage:message];
if (!_player.isFullScreen) return;
inputView.hidden = YES;
}
- (void)removeViews
{
[_player removeFromSuperview];
[_chatView removeFromSuperview];
[_inputView removeFromSuperview];
_player = nil;
_player.delegate = nil;
_chatView.delegate = nil;
_chatView = nil;
_inputView.delegate = nil;
_inputView = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)dismissKeyboard
{
[self.view endEditing:YES];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([NSStringFromClass([touch.view class]) isEqualToString:@"LVCChatView"]) {
return NO;
}
return YES;
}
#pragma mark -
#pragma mark - makeConstraints
- (void)makeConstraints
{
[self.player mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(20);
make.left.right.mas_equalTo(0);
make.height.equalTo(@(playerFrame.size.height));
}];
_inputView.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 49);
}
- (void)makePlayerViewConstraints:(BOOL)fullScreen
{
if (fullScreen) { //全屏
[self.player mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.bottom.mas_equalTo(0);
}];
_inputView.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 49);
_barrageBaseView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
} else { //半屏
[self.player mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(20);
make.left.right.mas_equalTo(0);
make.height.equalTo(@(playerFrame.size.height));
}];
_inputView.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 49);
}
}
#pragma mark -
#pragma mark - gatter
- (LVCPlayer *)player
{
if (!_player) {
_player = [[LVCPlayer alloc] initWithFrame:CGRectZero playerModel:self.model];
_player.delegate = self;
}
return _player;
}
- (LVCFullScreenPlayerController *)fullVC
{
if (!_fullVC) {
_fullVC = [[LVCFullScreenPlayerController alloc] init];
}
return _fullVC;
}
- (LVCInputView *)inputView
{
if (!_inputView) {
_inputView = [[LVCInputView alloc]init];
_inputView.delegate = self;
}
return _inputView;
}
- (LVCChatView *)chatView
{
if (!_chatView) {
_chatView = [[LVCChatView alloc] initWithFrame:CGRectMake(0, SCREEN_WIDTH*9/16+20, SCREEN_WIDTH, SCREEN_HEIGHT-SCREEN_WIDTH*9/16-20-49) roomId:_roomId];
_chatView.delegate = self;
}
return _chatView;
}
- (UIView *)barrageBaseView
{
if (!_barrageBaseView) {
_barrageBaseView = [[UIView alloc] init];
_barrageBaseView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_barrageBaseView];
}
return _barrageBaseView;
}
- (CNBarrageManager *)barrageManager
{
if (!_barrageManager)
{
self.barrageManager = [[CNBarrageManager alloc] init];
}
return _barrageManager;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
NSLog(@"LVCLivePlayerController dealloc");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end