JLChen
2021-02-03 4715e99a9be1c50d8ec31f594af9ebde18647c94
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
//
//  EZOnlineCameraCell.m
//  EZOpenSDKDemo
//
//  Created by yuqian on 2020/3/10.
//  Copyright © 2020 hikvision. All rights reserved.
//
 
#import "EZOnlineCameraCell.h"
#import "EZPlayer.h"
#import "HIKLoadView.h"
#import "Masonry.h"
#import "UIAlertController+TextField.h"
 
@interface EZOnlineCameraCell() <EZPlayerDelegate>
 
@property (nonatomic, strong) UIView *playView;
@property (nonatomic, strong) HIKLoadView *loadingView;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *playerPlayButton;
 
@property (nonatomic, strong) EZPlayer *player;
@property (nonatomic, copy) NSString *deviceSerial;
@property (nonatomic, assign) NSInteger cameraNo;
@property (nonatomic, copy) NSString *verifyCode;
 
@end
 
 
@implementation EZOnlineCameraCell
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self layoutUI];
    }
    return self;
}
 
- (void) startPlayWithDeviceSerial:(NSString *)deviceSerial cameraNo:(NSInteger)cameraNo verifyCode:(NSString *)code {
    
    if (deviceSerial.length == 0 ) {
        return;
    }
    
    self.deviceSerial = deviceSerial;
    self.cameraNo = cameraNo;
    self.verifyCode = [NSString stringWithFormat:@"%@", code];
    
    [self startPlay];
}
 
- (void)startPlay {
    
    if (_isPlaying) {
        [self stopPlay];
    }
    
    self.isPlaying = YES;
    
    _player = [EZOPENSDK createPlayerWithDeviceSerial:self.deviceSerial cameraNo:self.cameraNo];
    _player.delegate = self;
    
    if (self.verifyCode.length > 0) {
        [_player setPlayVerifyCode:self.verifyCode];
    }
    
    [_player setPlayerView:_playView];
    [_player startRealPlay];
    
    _playerPlayButton.hidden = YES;
    _loadingView.hidden = NO;
    [self.loadingView startSquareClcokwiseAnimation];
}
 
- (void)stopPlay {
    
    self.isPlaying = NO;
    _playerPlayButton.hidden = NO;
    [self.loadingView stopSquareClockwiseAnimation];
    
    [_player stopRealPlay];
}
 
- (void)setPlayerVerifyCode:(NSString *)code {
    
    [_player setPlayVerifyCode:code];
}
 
#pragma mark - PlayerDelegate Methods
- (void)player:(EZPlayer *)player didPlayFailed:(NSError *)error
{
    NSLog(@"player: %@, didPlayFailed: %@", player, error);
    //如果是需要验证码或者是验证码错误
    NSString *tip = NSLocalizedString(@"device_play_fail", @"播放失败");
    if (error.code == EZ_SDK_NEED_VALIDATECODE || error.code == EZ_SDK_VALIDATECODE_NOT_MATCH) {
 
        tip = @"设备验证码错误";
    }
    
    [_player stopRealPlay];
    
    self.isPlaying = NO;
    _playerPlayButton.hidden = NO;
    
    [self.loadingView stopSquareClockwiseAnimation];
    
    self.messageLabel.text = [NSString stringWithFormat:@"%@(%d)",tip, (int)error.code];
    self.messageLabel.hidden = NO;
}
 
- (void)player:(EZPlayer *)player didReceivedMessage:(NSInteger)messageCode
{
    NSLog(@"player: %@, didReceivedMessage: %d", player, (int)messageCode);
    if (messageCode == PLAYER_REALPLAY_START)
    {
        [self.loadingView stopSquareClockwiseAnimation];
        self.messageLabel.hidden = YES;
 
        [_player closeSound];
        NSLog(@"GetInnerPlayerPort:%d", [self.player getInnerPlayerPort]);
        NSLog(@"GetStreamFetchType:%d", [self.player getStreamFetchType]);
    }
    else if (messageCode == PLAYER_NET_CHANGED)
    {
        [_player stopRealPlay];
        [_player startRealPlay];
    }
}
 
#pragma mark - UI
 
- (void)layoutUI {
    
    [self.contentView addSubview:self.playView];
    [self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.contentView addSubview:self.loadingView];
    [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@14);
        make.center.equalTo(self.playView);
    }];
    
    [self.contentView addSubview:self.messageLabel];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
        make.left.right.equalTo(self.contentView);
    }];
    
    [self.contentView addSubview:self.playerPlayButton];
    [self.playerPlayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
        make.width.height.equalTo(@54);
    }];
}
 
- (UIView *)playView {
    
    if (!_playView) {
        _playView = [[UIView alloc]init];
        _playView.backgroundColor = [UIColor blackColor];
        _playView.layer.masksToBounds = YES;
        _playView.layer.cornerRadius = 10.0;
    }
    return _playView;
}
 
- (HIKLoadView *)loadingView {
    
    if(!_loadingView) {
        _loadingView = [[HIKLoadView alloc] initWithHIKLoadViewStyle:HIKLoadViewStyleSqureClockWise];
        _loadingView.hidden = YES;
    }
    return _loadingView;
}
 
- (UIButton *)playerPlayButton {
 
    if (!_playerPlayButton) {
        _playerPlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playerPlayButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        [_playerPlayButton addTarget:self action:@selector(startPlay) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playerPlayButton;
}
 
- (UILabel *)messageLabel {
    
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = [UIFont systemFontOfSize:12.0];
        _messageLabel.textColor = [UIColor whiteColor];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.hidden = YES;
    }
    return _messageLabel;
}
 
@end