JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
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
//
//  LocalPlayViewController.m
//  LCOpenSDKDemo
//
//  Created by 韩燕瑞 on 2020/7/2.
//  Copyright © 2020 lechange. All rights reserved.
//
 
#import "UIDevice+Lechange.h"
#import "LCOpenSDK_Prefix.h"
#import "LocalPlayViewController.h"
 
#define RECORD_BAR_HEIGHT 40.0
#define TIME_LAB_WIDTH 60.0
typedef NS_ENUM(NSInteger, PlayState) {
    Play = 0,
    Pause = 1,
    Stop = 2
};
 
@interface LocalPlayViewController ()
{
      UIImageView* m_playImg;
      UIView* m_playBarView;
      UIButton* m_playBtn;
      UIButton* m_scalBtn;
      UIButton* m_soundBtn;
      UILabel* m_startTimeLab;
      UILabel* m_endTimeLab;
      UISlider* m_playSlider;
      UILabel* m_tipLab;
      UIActivityIndicatorView* m_progressInd;
    
    LCOpenSDK_Utils* m_Utils;
 
    CGRect m_screenFrame;
    LCOpenSDK_PlayWindow* m_play;
      BOOL m_isSeeking;
    PlayState m_playState;
    long  m_fileTime;
}
 
@end
 
@implementation LocalPlayViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWindowView];
    [self.view bringSubviewToFront:m_playBarView];
    
    dispatch_queue_t playLocal = dispatch_queue_create("playLocal", nil);
    dispatch_async(playLocal, ^{
        m_playState = Stop;
        [self onPlay];
    });
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onResignActive:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onActive:)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
    signal(SIGPIPE, SIG_IGN);
}
 
- (void)initWindowView {
    self.view.backgroundColor = [UIColor whiteColor];
    m_screenFrame = [UIScreen mainScreen].bounds;
 
    UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:NSLocalizedString(LOCAL_PLAY_TITLE_TXT, nil)];
 
    UIButton* left = [UIButton buttonWithType:UIButtonTypeCustom];
    [left setFrame:CGRectMake(0, 0, 50, 30)];
    UIImage* img = [UIImage leChangeImageNamed:Back_Btn_Png];
 
    [left setBackgroundImage:img forState:UIControlStateNormal];
    [left addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBtn = [[UIBarButtonItem alloc] initWithCustomView:left];
    [item setLeftBarButtonItem:leftBtn animated:NO];
    [super.m_navigationBar pushNavigationItem:item animated:NO];
 
    [self.view addSubview:super.m_navigationBar];
 
    m_playImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, super.m_yOffset, m_screenFrame.size.width, m_screenFrame.size.width * 9 / 16)];
    [self.view addSubview:m_playImg];
    [self layOutBar];
 
    m_tipLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, m_screenFrame.size.width - 20, 20)];
    m_tipLab.center = CGPointMake(m_playImg.center.x, m_playImg.center.y + CGRectGetHeight(m_playImg.frame) / 2 + 50);
    [m_tipLab setBackgroundColor:[UIColor clearColor]];
    m_tipLab.textAlignment = NSTextAlignmentCenter;
    [m_tipLab setFont:[UIFont systemFontOfSize:15.0]];
    [self.view addSubview:m_tipLab];
    m_play = [[LCOpenSDK_PlayWindow alloc] initPlayWindow:CGRectMake(0, super.m_yOffset, m_screenFrame.size.width, m_screenFrame.size.width * 9 / 16) Index:1];
    [m_play setSurfaceBGColor:[UIColor blackColor]];
    [self.view addSubview:[m_play getWindowView]];
    
    m_progressInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    m_progressInd.center = CGPointMake(self.view.center.x, m_playImg.center.y);
    [self.view addSubview:m_progressInd];
    [m_progressInd startAnimating];
 
    [self.view bringSubviewToFront:m_playImg];
    [self.view bringSubviewToFront:m_playBarView];
    [self.view bringSubviewToFront:m_progressInd];
    [m_play setWindowListener:(id<LCOpenSDK_EventListener>)self];
 
    m_Utils = [[LCOpenSDK_Utils alloc] init];
    m_isSeeking = NO;
    signal(SIGPIPE, SIG_IGN);
}
 
- (void)layOutBar
{
    m_playBarView = [[UIView alloc] initWithFrame:CGRectMake(0, super.m_yOffset - RECORD_BAR_HEIGHT + m_playImg.frame.size.height, m_playImg.frame.size.width, RECORD_BAR_HEIGHT)];
    [m_playBarView setBackgroundColor:[UIColor grayColor]];
    m_playBarView.alpha = 0.5;
    [self.view addSubview:m_playBarView];
 
    m_playBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
    [m_playBtn setBackgroundImage:[UIImage leChangeImageNamed:VideoPlay_Play_Png] forState:UIControlStateNormal];
    [m_playBtn setBackgroundImage:[UIImage leChangeImageNamed:VideoPlay_Pause_Png] forState:UIControlStateSelected];
    [m_playBtn addTarget:self action:@selector(onPlay) forControlEvents:UIControlEventTouchUpInside];
    [m_playBarView addSubview:m_playBtn];
    
    
    m_soundBtn = [[UIButton alloc] initWithFrame:CGRectMake(RECORD_BAR_HEIGHT, 0,
                                                            RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
    [m_soundBtn setBackgroundImage:[UIImage leChangeImageNamed:Video_SoundOn_Png]
                          forState:UIControlStateNormal];
    [m_soundBtn setBackgroundImage:[UIImage leChangeImageNamed:Video_SoundOff_Png]
                          forState:UIControlStateSelected];
    [m_soundBtn addTarget:self
                   action:@selector(onSound:)
         forControlEvents:UIControlEventTouchUpInside];
    [m_playBarView addSubview:m_soundBtn];
    
 
    m_scalBtn = [[UIButton alloc] initWithFrame:CGRectMake(m_playBarView.frame.size.width - RECORD_BAR_HEIGHT, 0, RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
    [m_scalBtn setBackgroundImage:[UIImage leChangeImageNamed:VideoPlay_FullScreen_Png] forState:UIControlStateNormal];
    [m_scalBtn addTarget:self action:@selector(onFullScreen) forControlEvents:UIControlEventTouchUpInside];
    [m_playBarView addSubview:m_scalBtn];
 
    m_startTimeLab = [[UILabel alloc] initWithFrame:CGRectMake( 2 * RECORD_BAR_HEIGHT, 0, TIME_LAB_WIDTH, RECORD_BAR_HEIGHT)];
    [m_startTimeLab setBackgroundColor:[UIColor clearColor]];
    [m_startTimeLab setFont:[UIFont systemFontOfSize:12.0]];
    m_startTimeLab.textAlignment = NSTextAlignmentCenter;
    [m_playBarView addSubview:m_startTimeLab];
 
    m_endTimeLab = [[UILabel alloc] initWithFrame:CGRectMake(m_playBarView.frame.size.width - TIME_LAB_WIDTH - RECORD_BAR_HEIGHT, 0, TIME_LAB_WIDTH, RECORD_BAR_HEIGHT)];
    [m_endTimeLab setBackgroundColor:[UIColor clearColor]];
    [m_endTimeLab setFont:[UIFont systemFontOfSize:12.0]];
    m_endTimeLab.textAlignment = NSTextAlignmentCenter;
    [m_playBarView addSubview:m_endTimeLab];
 
    m_playSlider = [[UISlider alloc] initWithFrame:CGRectMake(RECORD_BAR_HEIGHT * 2+ TIME_LAB_WIDTH, 0, m_playBarView.frame.size.width - 2 * (RECORD_BAR_HEIGHT + TIME_LAB_WIDTH) - RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
    m_playSlider.value = m_playSlider.minimumValue;
    [m_playSlider addTarget:self action:@selector(openTouch) forControlEvents:UIControlEventTouchDown];
    [m_playSlider addTarget:self action:@selector(onSeek) forControlEvents:UIControlEventTouchUpInside];
    
    
    [m_playBarView addSubview:m_playSlider];
}
 
- (void)refreshSubView
{
    [m_playBtn setFrame:CGRectMake(0, 0, RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
    [m_soundBtn setFrame:CGRectMake(RECORD_BAR_HEIGHT, 0, RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
    [m_scalBtn setFrame:CGRectMake(m_playBarView.frame.size.width - RECORD_BAR_HEIGHT, 0, RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
    [m_startTimeLab setFrame:CGRectMake(RECORD_BAR_HEIGHT * 2, 0, TIME_LAB_WIDTH, RECORD_BAR_HEIGHT)];
    [m_endTimeLab setFrame:CGRectMake(m_playBarView.frame.size.width - TIME_LAB_WIDTH - RECORD_BAR_HEIGHT, 0, TIME_LAB_WIDTH, RECORD_BAR_HEIGHT)];
    [m_playSlider setFrame:CGRectMake(RECORD_BAR_HEIGHT * 2+ TIME_LAB_WIDTH, 0, m_playBarView.frame.size.width - 2 * (RECORD_BAR_HEIGHT + TIME_LAB_WIDTH) - RECORD_BAR_HEIGHT, RECORD_BAR_HEIGHT)];
}
 
- (void)onPlay {
    dispatch_async(dispatch_get_main_queue(), ^{
        m_isSeeking = NO;
        if (m_playState == Stop) {
            [m_play stopFile:NO];
            [m_play playFile:_filepath];
            m_playBtn.selected = YES;
        }
        else if (m_playState == Play) {
            [m_play pause];
            m_playBtn.selected = NO;
            m_playState = Pause;
        }
        else {
            [m_play resume];
            m_playBtn.selected = YES;
        }
    });
}
 
#pragma mark - 声音开关
- (void)onSound:(UIButton *)sender {
    if (m_playState != Play) {
        return;
    }
    if (sender.isSelected) {
        [m_play playAudio];
    }
    else  {
        [m_play stopAudio];
    }
    sender.selected = !sender.isSelected;
}
 
#pragma mark - 拖动
- (void)openTouch {
    m_isSeeking = YES;
}
 
- (void)onSeek {
    [m_progressInd startAnimating];
    if (Pause == m_playState) {
        [m_play resume];
    }
 
    m_playState = Play;
    NSInteger seektime = m_fileTime * m_playSlider.value;
    [m_play seek:seektime];
}
 
#pragma mark - 全屏
- (void)onFullScreen {
    [UIDevice lc_setRotateToSatusBarOrientation];
}
 
#pragma mark - 双击屏幕
- (void)onWindowDBClick:(CGFloat)dx dy:(CGFloat)dy Index:(NSInteger)index {
    m_playBarView.hidden = !m_playBarView.hidden;
}
 
#pragma mark - 播放回调
- (void)onPlayerResult:(NSString*)code Type:(NSInteger)type Index:(NSInteger)index {
    
}
 
#pragma mark - 开始时间结束时间
- (void)onFileTime:(long)beginTime EndTime:(long)endTime Index:(NSInteger)index {
    m_fileTime = endTime - beginTime;
    NSString *benginTimeStr = [self transformTime:0];
    NSString *endTimeStr = [self transformTime:m_fileTime];
    dispatch_async(dispatch_get_main_queue(), ^{
        m_startTimeLab.text = benginTimeStr;
        m_endTimeLab.text = endTimeStr;
    });
}
 
- (NSString *)transformTime:(long)time {
    NSDate * myDate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter * formatter=[[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0000"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *timeStr=[formatter stringFromDate:myDate];
    return timeStr;
}
 
#pragma mark - 录像开始播放回调
- (void)onPlayBegan:(NSInteger)index {
    dispatch_async(dispatch_get_main_queue(), ^{
        m_playState = Play;
        m_isSeeking = NO;
        m_playBtn.selected = YES;
        [m_progressInd stopAnimating];
        m_playSlider.enabled = YES;
    });
}
 
#pragma mark - 录像播放结束回调
- (void)onPlayFinished:(NSInteger)index {
    dispatch_async(dispatch_get_main_queue(), ^{
        m_playState = Stop;
        m_playBtn.selected = NO;
        m_playSlider.enabled = NO;
        m_tipLab.text = @"play over";
    });
}
 
#pragma mark - 录像时间状态回调
- (void)onPlayerTime:(long)time Index:(NSInteger)index {
    if (YES == m_isSeeking) {
           return;
    }
    double percent = (double)time / 100000;
    NSInteger currentTime = percent * m_fileTime;
    dispatch_async(dispatch_get_main_queue(), ^{
        m_playSlider.value = percent;
        m_startTimeLab.text = [NSString stringWithFormat:@"%@", [self transformTime:currentTime]];
    });
}
 
#pragma mark - 返回
- (void)onBack {
    [m_play stopFile:NO];
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
 
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self layoutViews:toInterfaceOrientation force:NO];
}
 
- (void)viewWillLayoutSubviews {
    NSLog(@"do nothing, but rewrite method! ");
}
 
- (void)layoutViews:(UIInterfaceOrientation)InterfaceOrientation force:(BOOL)beForce {
    CGFloat width = [[[UIDevice currentDevice] systemVersion] floatValue] < 7 ? m_screenFrame.size.width - 20 : m_screenFrame.size.width;
    if (UIInterfaceOrientationIsPortrait(InterfaceOrientation)) {
        [m_scalBtn setBackgroundImage:[UIImage leChangeImageNamed:VideoPlay_FullScreen_Png] forState:UIControlStateNormal];
        [m_play setWindowFrame:CGRectMake(0, super.m_yOffset, m_screenFrame.size.width, m_screenFrame.size.width * 9 / 16)];
        m_playImg.frame = CGRectMake(0, super.m_yOffset, m_screenFrame.size.width, m_screenFrame.size.width * 9 / 16);
        m_progressInd.center = m_playImg.center;
        m_playBarView.frame = CGRectMake(0, super.m_yOffset + m_playImg.frame.size.height - RECORD_BAR_HEIGHT, m_playImg.frame.size.width, RECORD_BAR_HEIGHT);
        [self refreshSubView];
        super.m_navigationBar.hidden = NO;
    }
    else {
        [m_scalBtn setBackgroundImage:[UIImage leChangeImageNamed:VideoPlay_SmallScreen_Png] forState:UIControlStateNormal];
        [m_play setWindowFrame:CGRectMake(0, 0, m_screenFrame.size.height, width)];
        m_playImg.frame = CGRectMake(0, 0, m_screenFrame.size.height, width);
        m_progressInd.center = m_playImg.center;
        m_playBarView.frame = CGRectMake(0, width - RECORD_BAR_HEIGHT, m_screenFrame.size.height, RECORD_BAR_HEIGHT);
        [self refreshSubView];
        [self.view bringSubviewToFront:m_playBarView];
        super.m_navigationBar.hidden = YES;
    }
}
 
- (BOOL)shouldAutorotate {
    return YES;
}
 
- (void)onActive:(id)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self onPlay];
    });
}
 
- (void)onResignActive:(id)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        [m_play stopFile:NO];
        m_playState = Stop;
        m_playBtn.selected = YES;
    });
}
 
@end