LVCPlayer.m
20.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//
// LVCPlayer.m
// LiveVideoCloud
//
// Created by iOS on 17/3/13.
// Copyright © 2017年 cnlive. All rights reserved.
//
#import "LVCPlayer.h"
#import "LVCPlayerLoadingView.h"
#import "SevenSwitch.h"
@interface LVCPlayer ()<UIGestureRecognizerDelegate,LVCPlayerLoadingViewDelegate>
{
LVCChannelDetailsModel *_model;
}
@property (nonatomic, strong) CNLiveMoviePlayerController *player;
@property (nonatomic, strong) UIButton *backBtn; //返回
@property (nonatomic, strong) UIButton *playOrPauseBtn; //播放/暂停
@property (nonatomic, strong) UILabel *titleLab; //标题
@property (nonatomic, strong) UIButton *fullScreenBtn; //控制全屏按钮
@property (nonatomic, strong) UITapGestureRecognizer *singleTap; //单击手势
@property (nonatomic, strong) LVCPlayerLoadingView *loadingView; //加载图
@property (nonatomic, strong) SevenSwitch *barrageSwitch; //弹幕开关
@property (nonatomic, strong) UIButton *editBtn; //全屏编辑按钮
@property (nonatomic, strong) NSTimer *timer; //控制上下工具条定时器
@end
@implementation LVCPlayer
- (void)dealloc
{
NSLog(@"LVCPlayer dealloc");
}
- (instancetype)initWithFrame:(CGRect)frame playerModel:(LVCChannelDetailsModel *)model
{
self = [super initWithFrame:frame];
if (self) {
_model = model;
[self initPlayerWithModel:model];
[self addSubview:self.contentView];
[self.contentView addSubview:self.bottomBgView];
[self.contentView addSubview:self.topBgView];
[self.bottomBgView addSubview:self.playOrPauseBtn];
[self.bottomBgView addSubview:self.fullScreenBtn];
[self.topBgView addSubview:self.titleLab];
[self.topBgView addSubview:self.barrageSwitch];
[self addSubview:self.backBtn];
[self.contentView addSubview:self.loadingView];
[self.contentView addSubview:self.editBtn];
[self.contentView addGestureRecognizer:self.singleTap];
[self makeSubViewsConstraints];
}
return self;
}
#pragma mark -
#pragma mark - initPlayer
- (void)initPlayerWithModel:(LVCChannelDetailsModel *)model
{
/*
*/
NSString *channelId = [NSString stringWithFormat:@"82_%@",model.mediaId];
_player = [[CNLiveMoviePlayerController alloc] initLivePlayWithChannelId:channelId anchorStatus:NO authSuccess:^{
[self addObservers];
_player.videoDecoderMode = CNLiveMovieVideoDecoderMode_AUTO;
_player.shouldAutoplay = YES;
[_player prepareToPlay];
[self.contentView addSubview:_player.view];
[self.contentView sendSubviewToBack:_player.view];
[self makePlayerConstraint];
} authFailed:^(NSDictionary *errorInfo) {
NSLog(@"初始化点播播放器失败%@", errorInfo);
[SVProgressHUD showInfoWithStatus:@"抱歉,加载失败~"];
[_loadingView stopLoading];
} getAnchorStatusResult:NULL];
/*
//点播播放
_player = [[CNLiveMoviePlayerController alloc] initVodPlayWithVId:@"aaa123"
authSuccess:^{
[self addObservers];
_player.videoDecoderMode = CNLiveMovieVideoDecoderMode_AUTO;
_player.shouldAutoplay = YES;
[_player prepareToPlay];
[self.contentView addSubview:_player.view];
[self.contentView sendSubviewToBack:_player.view];
[self makePlayerConstraint];
} authFailed:^(NSDictionary *errorInfo) {
NSLog(@"初始化点播播放器失败%@", errorInfo);
}];
*/
}
#pragma mark -
#pragma mark - action
- (void)playOrPauseBtnAction:(UIButton *)btn
{
if (btn.selected) {
[_player pause];
} else {
[_player play];
}
btn.selected =! btn.selected;
}
- (void)backBtnAction:(UIButton *)btn
{
if (self.delegate && [self.delegate respondsToSelector:@selector(lvcPlayer:clickedBackBtn:)]) {
[self.delegate lvcPlayer:self clickedBackBtn:btn];
}
}
- (void)fullScreenBtnAction:(UIButton *)btn
{
if (self.delegate && [self.delegate respondsToSelector:@selector(lvcPlayer:clickedFullScreenBtn:)]) {
[self.delegate lvcPlayer:self clickedFullScreenBtn:btn];
}
}
- (void)barrageSwitchAction
{
self.editBtn.hidden =! self.barrageSwitch.on;
if (self.delegate && [self.delegate respondsToSelector:@selector(lvcPlayer:barrageSwitch:)]) {
[self.delegate lvcPlayer:self barrageSwitch:_barrageSwitch.on];
}
}
- (void)editBtnAction:(UIButton *)btn
{
if (self.hideBlock) {
self.hideBlock(NO);
}
[self hideControlView];
if (self.delegate && [self.delegate respondsToSelector:@selector(lvcPlayer:clickedEditBtn:)]) {
[self.delegate lvcPlayer:self clickedEditBtn:btn];
}
}
- (void)singleTapView
{
[UIView animateWithDuration:0.25 animations:^{
_topBgView.alpha = _topBgView.alpha == 0 ? 1 : 0;
_bottomBgView.alpha = _bottomBgView.alpha == 0 ? 1 : 0;
_editBtn.alpha = _editBtn.alpha == 0 ? 1 : 0;
}];
[self autoHideControlView];
if (_isFullScreen) {
[[UIApplication sharedApplication] setStatusBarHidden:self.topBgView.alpha == 0 ? YES : NO withAnimation:UIStatusBarAnimationFade];
if (self.hideBlock) {
self.hideBlock(YES);
}
}
}
- (void)autoHideControlView
{
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(hideControlView) userInfo:nil repeats:NO];
} else {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[_timer invalidate];
_timer = nil;
}
}
- (void)hideControlView
{
[UIView animateWithDuration:0.25 animations:^{
_topBgView.alpha = 0;
_bottomBgView.alpha = 0;
_editBtn.alpha = 0;
}];
if (_isFullScreen) {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}
if (_timer) {
[_timer invalidate];
_timer = nil;
}
}
#pragma mark -
#pragma mark - LVCPlayerLoadingViewDelegate
- (void)retryBtnClick
{
[_loadingView startLoading];
[_player reload:nil];
}
#pragma mark - UIGestureRecognizerDelegate 手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([NSStringFromClass([touch.view class]) isEqualToString:@"SevenSwitch"]) {
return NO;
}
return YES;
}
#pragma mark -
#pragma mark - Notification
- (void)addObservers
{
//视频加载状态改变
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
//视频播放错误及结束原因
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//视频缓冲至可播放状态
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mediaIsPreparedToPlayDidChange:)
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:nil];
//视频播放状态改变
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)loadStateDidChange:(NSNotification *)notification
{
if (_player) {
MPMovieLoadState loadState = _player.loadState;
if ((loadState & MPMovieLoadStatePlaythroughOK) != 0) {
NSLog(@"loadStateDidChange: MPMovieLoadStatePlaythroughOK: %d\n", (int)loadState);
} else if ((loadState & MPMovieLoadStateStalled) != 0) {
NSLog(@"loadStateDidChange: MPMovieLoadStateStalled: %d\n", (int)loadState);
} else {
NSLog(@"loadStateDidChange: ???: %d\n", (int)loadState);
}
}
}
- (void)moviePlayBackDidFinish:(NSNotification *)notification
{
if (_player) {
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (MPMovieFinishReasonPlaybackError == reason) {
_loadingView.hidden = NO;
} else {
_loadingView.hidden = YES;
}
switch (reason)
{
case MPMovieFinishReasonPlaybackEnded:
NSLog(@"playbackStateDidChange: MPMovieFinishReasonPlaybackEnded: %d\n", reason);
break;
case MPMovieFinishReasonUserExited:
NSLog(@"playbackStateDidChange: MPMovieFinishReasonUserExited: %d\n", reason);
break;
case MPMovieFinishReasonPlaybackError:
NSLog(@"playbackStateDidChange: MPMovieFinishReasonPlaybackError: %d\n", reason);
break;
default:
NSLog(@"playbackPlayBackDidFinish: ???: %d\n", reason);
break;
}
}
}
- (void)mediaIsPreparedToPlayDidChange:(NSNotification *)notification
{
if (_player) {
NSLog(@"isPreparedToPlay");
}
}
- (void)moviePlayBackStateDidChange:(NSNotification *)notification
{
if (_player) {
if (_player.playbackState == MPMoviePlaybackStatePlaying) {
_playOrPauseBtn.selected = YES;
_loadingView.hidden = YES;
} else {
_playOrPauseBtn.selected = NO;
}
NSLog(@"moviePlayBackStateDidChange : %ld",(long)_player.playbackState);
/*
switch (_player.playbackState) {
case MPMoviePlaybackStateStopped: {
[self changePlayBtnStatus:NO];
NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateStopped");
break;
}
case MPMoviePlaybackStatePlaying: {
[self changePlayBtnStatus:YES];
NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStatePlaying");
break;
}
case MPMoviePlaybackStatePaused:
[self changePlayBtnStatus:NO];
NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStatePaused");
break;
case MPMoviePlaybackStateInterrupted:
[self changePlayBtnStatus:NO];
NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateInterrupted");
break;
case MPMoviePlaybackStateSeekingForward:
[self changePlayBtnStatus:NO];
NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateSeekingForward");
break;
case MPMoviePlaybackStateSeekingBackward:
[self changePlayBtnStatus:NO];
NSLog(@"moviePlayBackStateDidChange : MPMoviePlaybackStateSeekingBackward");
break;
default:
[self changePlayBtnStatus:NO];
NSLog(@"moviePlayBackStateDidChange : %ld", _player.playbackState);
break;
}
*/
}
}
- (void)appResignActive:(NSNotification *)noti
{
[_player pause];
}
- (void)appBecomeActive:(NSNotification *)noti
{
[_player play];
}
#pragma mark -
#pragma mark - public
- (void)stopPlaying
{
if (_player.isPlaying) {
[_player stop];
}
_player = nil;
if (_timer) {
[_timer invalidate];
_timer = nil;
}
[self removeMovieNotificationObservers];
}
- (void)removeMovieNotificationObservers
{
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
}
#pragma mark -
#pragma mark - makeConstraints
- (void)makePlayerConstraint
{
[self.player.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self.contentView);
make.height.equalTo(self.contentView);
make.center.equalTo(self.contentView);
}];
}
- (void)makeSubViewsConstraints
{
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.bottom.mas_equalTo(0);
}];
[self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.contentView);
make.width.mas_equalTo(100);
make.height.mas_equalTo(80);
}];
[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.topBgView).with.offset(10);
make.width.height.mas_equalTo(30);
make.top.equalTo(self.topBgView).with.offset(10);
}];
[self.topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(0);
make.height.mas_equalTo(70);
}];
[self.barrageSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(35);
make.height.mas_equalTo(20);
make.top.mas_equalTo(20);
make.right.equalTo(self.topBgView).offset(-60);
}];
[self.bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.left.right.equalTo(self.contentView).with.offset(0);
make.height.mas_equalTo(50);
}];
[self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backBtn).offset(40);
make.top.mas_equalTo(15);
make.height.mas_equalTo(20);
make.right.equalTo(self.topBgView).offset(-100);
}];
[self.playOrPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.equalTo(self.bottomBgView).with.offset(0);
make.width.height.mas_equalTo(50);
}];
[self.fullScreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.bottom.equalTo(self.bottomBgView).with.offset(0);
make.width.height.mas_equalTo(50);
}];
[self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(40);
make.centerY.equalTo(self.contentView);
make.right.equalTo(self.contentView).with.offset(-20);
}];
}
#pragma mark -
#pragma mark - setter
- (void)setIsFullScreen:(BOOL)isFullScreen
{
_isFullScreen = isFullScreen;
if (isFullScreen) {
_barrageSwitch.hidden = NO;
_editBtn.hidden = NO;
[self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.topBgView).with.offset(20);
}];
[self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(25);
}];
} else {
_barrageSwitch.hidden = YES;
_editBtn.hidden = YES;
[self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.topBgView).with.offset(10);
}];
[self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(15);
}];
}
}
- (void)exitPlayer {
if (_player.isPlaying) {
[_player stop];
}
[_player destroyPlayer];
_player = nil;
[self removeMovieNotificationObservers];
if (_timer) {
[_timer invalidate];
_timer = nil;
}
}
#pragma mark -
#pragma mark - getter
- (BOOL)displayBarrage
{
return _barrageSwitch.on;
}
- (UIImageView *)contentView
{
if (!_contentView) {
_contentView = [[UIImageView alloc] init];
_contentView.backgroundColor = [UIColor blackColor];
_contentView.userInteractionEnabled = YES;
}
return _contentView;
}
- (UIImageView *)topBgView
{
if (!_topBgView) {
_topBgView = [[UIImageView alloc] init];
_topBgView.image = [UIImage imageNamed:@"top_shadow"];
_topBgView.userInteractionEnabled = YES;
}
return _topBgView;
}
- (UIImageView *)bottomBgView
{
if (!_bottomBgView) {
_bottomBgView = [[UIImageView alloc] init];
_bottomBgView.image = [UIImage imageNamed:@"bottom_shadow"];
_bottomBgView.userInteractionEnabled = YES;
}
return _bottomBgView;
}
- (UIButton *)playOrPauseBtn
{
if (!_playOrPauseBtn) {
_playOrPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_playOrPauseBtn setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
[_playOrPauseBtn setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateSelected];
[_playOrPauseBtn addTarget:self action:@selector(playOrPauseBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _playOrPauseBtn;
}
- (UILabel *)titleLab
{
if (!_titleLab) {
_titleLab = [[UILabel alloc] init];
_titleLab.text = _model.title;
_titleLab.textColor = [UIColor whiteColor];
_titleLab.font = [UIFont systemFontOfSize:17];
}
return _titleLab;
}
- (UIButton *)fullScreenBtn
{
if (!_fullScreenBtn) {
_fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_fullScreenBtn setImage:[UIImage imageNamed:@"fullscreen"] forState:UIControlStateNormal];
[_fullScreenBtn setImage:[UIImage imageNamed:@"nonfullscreen"] forState:UIControlStateSelected];
[_fullScreenBtn addTarget:self action:@selector(fullScreenBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _fullScreenBtn;
}
- (UIButton *)backBtn
{
if (!_backBtn) {
_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_backBtn setImage:[UIImage imageNamed:@"play_back"] forState:UIControlStateNormal];
[_backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _backBtn;
}
- (LVCPlayerLoadingView *)loadingView
{
if (!_loadingView) {
_loadingView = [[LVCPlayerLoadingView alloc] init];
}
return _loadingView;
}
- (UITapGestureRecognizer *)singleTap
{
if (!_singleTap) {
_singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapView)];
_singleTap.delegate = self;
}
return _singleTap;
}
- (SevenSwitch *)barrageSwitch { //弹幕开关
if (!_barrageSwitch) {
_barrageSwitch = [[SevenSwitch alloc] init];
_barrageSwitch.knobOnImage = [UIImage imageNamed:@"弹off"];
_barrageSwitch.knobOffImage = [UIImage imageNamed:@"弹off"];
_barrageSwitch.onColor = [UIColor yellowColorZX];
_barrageSwitch.borderColor = [UIColor blackColor];
_barrageSwitch.activeColor = [UIColor blackColor];
_barrageSwitch.inactiveColor = [UIColor blackColor];
_barrageSwitch.on = YES;
_barrageSwitch.hidden = YES;
[_barrageSwitch addTarget:self action:@selector(barrageSwitchAction) forControlEvents:UIControlEventValueChanged];
}
return _barrageSwitch;
}
- (UIButton *)editBtn
{
if (!_editBtn) {
_editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_editBtn setImage:[UIImage imageNamed:@"Barrage input"] forState:UIControlStateNormal];
[_editBtn addTarget:self action:@selector(editBtnAction:) forControlEvents:UIControlEventTouchUpInside];
_editBtn.hidden = YES;
}
return _editBtn;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end