CNLivePlayerViewController.m
10.4 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
//
// CNLivePlayerViewController.m
// QNLiveStreamingDemo
//
// Created by iOS on 16/12/29.
// Copyright © 2016年 zhulin. All rights reserved.
//
#import "CNLivePlayerViewController.h"
#import "PlayerFunctionView.h"
#import "LoadingView.h"
#import "BackgroundView.h"
@interface CNLivePlayerViewController ()<CNLiveMoviePlayerKitDelegate>
{
NSTimer *_currentTimer;
BOOL _isGetAnchorStatusError;
}
@property (nonatomic, strong) CNLiveMoviePlayerKit *player;
@property (nonatomic, strong) PlayerFunctionView *playFuncView;
@property (nonatomic, strong) BackgroundView *bgView;
@property (nonatomic, strong) LoadingView *loadingView;
@property (nonatomic, strong) UIButton *quitBtn;
@end
@implementation CNLivePlayerViewController
- (void)dealloc
{
_player.delegate = nil;
_player = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
_currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
if (_type == PlayerTypeLiving) { //直播
_player = [[CNLiveMoviePlayerKit alloc] initLivePlayWithChannelId:@"test001" authSuccess:^{
[self setupUI];
} authFailure:^(NSDictionary *errorInfo) {
NSLog(@"直播播放失败-------%@",errorInfo);
[_loadingView stopLoading];
} getAnchorStatusResult:^(BOOL result) {
if (!result) {
NSLog(@"无法获取主播状态-------------");
_isGetAnchorStatusError = YES;
_loadingView.retryBtn.hidden = NO;
}
}];
_player.delegate = self;
} else if (_type == PlayerTypeVOD) { //点播
_player = [[CNLiveMoviePlayerKit alloc] initVodPlayWithVId:@"test001" authSuccess:^{
[self setupUI];
_currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
} authFailure:^(NSDictionary *errorInfo) {
NSLog(@"点播播放失败-------%@",errorInfo);
[_loadingView stopLoading];
_loadingView.retryBtn.hidden = NO;
}];
_player.delegate = self;
} else { //普通播放
_player = [[CNLiveMoviePlayerKit alloc] initPlayerWithContentURL:self.videoUrlString];
[self setupUI];
_player.delegate = self;
}
[self.view addSubview:self.bgView];
[self.view addSubview:self.loadingView];
[self.view addSubview:self.playFuncView];
[self.view addSubview:self.quitBtn];
//注册通知
[self addObservers];
}
- (void)setupUI
{
if (_player.status != CNLivePlayerStatusError) {
UIView *playerView = _player.playerView;
playerView.backgroundColor = [UIColor blackColor];
playerView.contentMode = UIViewContentModeScaleAspectFit;
playerView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT);
[self.view addSubview:playerView];
[self.view sendSubviewToBack:playerView];
[_player play];
}
}
#pragma mark -
#pragma mark - action
- (void)playBtnAction:(UIButton *)button
{
button.selected = !button.selected;
if (!button.selected) {
[self.player pause];
} else {
[self.player play];
}
}
- (void)quitBtnAction
{
if (_player.status != CNLivePlayerStatusStopped) {
[_player stop];
[_player.playerView removeFromSuperview];
_player = nil;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)retryBtnAction
{
_loadingView.retryBtn.hidden = YES;
[_loadingView startLoading];
//调用重试方法
if (_isGetAnchorStatusError) {
[_player retryToGetAnchorStatusResult:^(BOOL result) {
if (!result) {
_loadingView.retryBtn.hidden = NO;
} else {
_isGetAnchorStatusError = NO;
}
[_loadingView stopLoading];
}];
}
[_player play];
}
- (void)beginScrubbing
{
[self.player pause];
}
- (void)endScrubbing
{
CGFloat duration = ceilf(CMTimeGetSeconds(self.player.totalDuration));
CMTime time = CMTimeMakeWithSeconds(_playFuncView.timeSlide.value*duration, 30);
[self.player seekTo:time];
}
- (void)timeChange
{
_playFuncView.timeSlide.value = CMTimeGetSeconds(_player.currentTime)/CMTimeGetSeconds(_player.totalDuration);
_playFuncView.currentTimeLabel.text = [self getMMSSFromSS:[NSString stringWithFormat:@"%f",CMTimeGetSeconds(_player.currentTime)]];
_playFuncView.durationLabel.text = [self getMMSSFromSS:[NSString stringWithFormat:@"%f",CMTimeGetSeconds(_player.totalDuration)]];
}
-(NSString *)getMMSSFromSS:(NSString *)totalTime {
NSInteger seconds = [totalTime integerValue];
NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
return format_time;
}
- (void)willEnterBackground
{
[_player pause];
}
- (void)willBackFromBackground
{
[_player resume];
}
#pragma mark -
#pragma mark - Notification
- (void)addObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willBackFromBackground)
name:UIApplicationDidBecomeActiveNotification
object:nil];
//获取主播状态通知(点播和普通播放不需要注册该通知)
if (_type != PlayerTypeLiving) return;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(anchorStatusChanged:)
name:CNLiveAnchorStatusChangedNotification
object:nil];
}
- (void)anchorStatusChanged:(NSNotification *)notification
{
[self.loadingView stopLoading];
_bgView.anchorStatus = _player.anchorStatus;
}
- (void)anchorStatusConnectFailed
{
//重新连接获取主播状态
_loadingView.retryBtn.hidden = NO;
}
#pragma mark -
#pragma mark - CNLiveMoviePlayerKitDelegate
- (void)player:(CNLiveMoviePlayerKit *)player statusDidChanged:(CNLivePlayerStatus)state
{
switch (state) {
case CNLivePlayerStatusReady:
_playFuncView.playOrPauseBtn.selected = YES;
break;
case CNLivePlayerStatusCaching:
[_loadingView startLoading];
break;
case CNLivePlayerStatusPlaying:
[_loadingView stopLoading];
break;
case CNLivePlayerStatusStopped:
[_loadingView stopLoading];
_playFuncView.playOrPauseBtn.selected = NO;
if (_currentTimer) {
[_currentTimer invalidate];
_currentTimer = nil;
}
break;
case CNLivePlayerStatusError:
_playFuncView.playOrPauseBtn.selected = NO;
_loadingView.retryBtn.hidden = NO;
break;
case CNLivePlayerStateAutoReconnecting:
_playFuncView.playOrPauseBtn.selected = NO;
break;
default:
break;
}
}
- (void)player:(CNLiveMoviePlayerKit *)player stoppedWithError:(NSError *)error
{
NSLog(@"error ----- %@",error);
[_loadingView stopLoading];
_loadingView.retryBtn.hidden = NO;
_playFuncView.playOrPauseBtn.selected = NO;
}
- (void)player:(CNLiveMoviePlayerKit *)player codecError:(NSError *)error
{
NSLog(@"解码器错误error ----- %@",error);
[_loadingView stopLoading];
_loadingView.retryBtn.hidden = NO;
}
#pragma mark -
#pragma mark - getter
- (PlayerFunctionView *)playFuncView
{
if (!_playFuncView) {
_playFuncView = [[PlayerFunctionView alloc] initWithFrame:CGRectMake(0, kSCREEN_HEIGHT-60, kSCREEN_WIDTH, 60)];
[_playFuncView.playOrPauseBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[_playFuncView.timeSlide addTarget:self action:@selector(beginScrubbing) forControlEvents:UIControlEventTouchDown];
[_playFuncView.timeSlide addTarget:self action:@selector(endScrubbing) forControlEvents:UIControlEventTouchUpInside];
[_playFuncView.timeSlide addTarget:self action:@selector(endScrubbing) forControlEvents:UIControlEventTouchUpOutside];
}
return _playFuncView;
}
- (LoadingView *)loadingView
{
if (!_loadingView) {
_loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
_loadingView.center = self.view.center;
[_loadingView.retryBtn addTarget:self action:@selector(retryBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _loadingView;
}
- (BackgroundView *)bgView
{
if (!_bgView) {
_bgView = [[BackgroundView alloc] initWithFrame:self.view.bounds];
_bgView.hidden = YES;
}
return _bgView;
}
- (UIButton *)quitBtn
{
if (!_quitBtn) {
_quitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_quitBtn.frame = CGRectMake(CGRectGetWidth(self.view.frame)-45, 25, 30, 30);
[_quitBtn setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
[_quitBtn addTarget:self action:@selector(quitBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _quitBtn;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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