MusicPlayerView.m
16.5 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
//
// MusicPlayerView.m
// DreamSleep
//
// Created by peter on 2022/5/7.
//
#import "MusicPlayerView.h"
#import "SPButton.h"
#import "TimingView.h"
@interface MusicPlayerView () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIButton *dismissBtn;
@property (nonatomic, strong) UIImageView *audioPicIV;
@property (nonatomic, strong) UILabel *audioNameLab;
@property (nonatomic, strong) UISlider *progressV;
@property (nonatomic, strong) UILabel *proLeftLb;
@property (nonatomic, strong) UILabel *proRightLb;
@property (nonatomic, strong) UIButton *playerBtn;
@property (nonatomic, strong) UIButton *leftPlayBtn;
@property (nonatomic, strong) UIButton *rightPlayBtn;
@property (nonatomic, strong) SPButton *playerListBtn;
@property (nonatomic, strong) SPButton *playModeBtn;
@property (nonatomic, strong) SPButton *timeCloseBtn;
@property (nonatomic, strong) TimingView *timingView;
@property (nonatomic, assign) NSInteger minuteIndex;
@property (nonatomic, assign) NSInteger countTime;
@property (nonatomic, assign) NSInteger totalTime;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation MusicPlayerView {
UITapGestureRecognizer *_tapGesture;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]) {
self.minuteIndex = 0;
self.countTime = 60;
self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
[self buildInterface];
}
return self;
}
- (void)buildInterface {
[self addSubview:self.dismissBtn];
[self addSubview:self.audioPicIV];
[self addSubview:self.audioNameLab];
[self addSubview:self.progressV];
[self addSubview:self.proLeftLb];
[self addSubview:self.proRightLb];
[self addSubview:self.playerBtn];
[self addSubview:self.leftPlayBtn];
[self addSubview:self.rightPlayBtn];
[self addSubview:self.playerListBtn];
[self addSubview:self.playModeBtn];
[self addSubview:self.timeCloseBtn];
[self.audioPicIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.width.height.equalTo(@144);
make.top.equalTo(self.mas_top).offset(160);
}];
[self.audioNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.audioPicIV.mas_bottom).offset(18);
make.centerX.equalTo(self.mas_centerX);
make.left.equalTo(self.mas_left);
make.right.equalTo(self.mas_right);
}];
[self.progressV mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(38);
make.right.equalTo(self.mas_right).offset(-37);
make.height.equalTo(@15);
make.bottom.equalTo(self.playerBtn.mas_top).offset(-40);
}];
[self.proLeftLb mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.progressV.mas_left);
make.top.equalTo(self.progressV.mas_bottom).offset(6);
make.height.equalTo(@17);
make.width.equalTo(@100);
}];
[self.proRightLb mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.progressV.mas_right);
make.top.equalTo(self.progressV.mas_bottom).offset(6);
make.height.equalTo(@17);
make.width.equalTo(@100);
}];
[self.playerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX);
make.width.equalTo(@50);
make.height.equalTo(@50);
make.bottom.equalTo(self.playerListBtn.mas_top).offset(-25);
}];
[self.leftPlayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.playerBtn.mas_centerY);
make.width.equalTo(@30);
make.height.equalTo(@30);
make.right.equalTo(self.playerBtn.mas_left).offset(-60);
}];
[self.rightPlayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.playerBtn.mas_centerY);
make.width.equalTo(@30);
make.height.equalTo(@30);
make.left.equalTo(self.playerBtn.mas_right).offset(60);
}];
[self.playerListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).offset(-36);
make.left.equalTo(self.mas_left).offset(23);
make.width.equalTo(@80);
make.height.equalTo(@60);
}];
[self.playModeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).offset(-36);
make.centerX.equalTo(self.mas_centerX);
make.width.equalTo(@60);
make.height.equalTo(@60);
}];
[self.timeCloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).offset(-36);
make.right.equalTo(self.mas_right).offset(-23);
make.width.equalTo(@80);
make.height.equalTo(@60);
}];
}
- (void)dealloc {
[self.timer invalidate];
self.timer = nil;
[self.timingView removeFromSuperview];
self.timingView = nil;
}
- (void)dismiss {
[self.ds_viewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)updatePlayerView:(SubAudioModel *)model {
_audioModel = model;
[self.audioPicIV yy_setImageWithURL:[NSURL URLWithString:model.audio_pic] placeholder:[UIImage imageNamed:@"basicPlaceholder"]];
self.audioNameLab.text = model.audio_name;
}
- (void)updateProgress:(float)progress currentTime:(NSString *)currentTime totalTime:(NSString *)totalTime {
self.progressV.value = progress;
self.proLeftLb.text = currentTime;
self.proRightLb.text = totalTime;
}
#pragma mark - 点击播放列表
- (void)didClickPlaylistAction:(UIButton *)sender {
}
#pragma mark - 切换播放模式
- (void)switchPlayModeAction:(UIButton *)sender {
SoundPlayMode mode = sender.tag;
NSString *title = sender.titleLabel.text;
NSString *normalImgName = @"single_play";
NSString *dkImgName = @"dk_single_play";
if (mode == SoundPlayModeSingle) {
mode = SoundPlayModeCycle;
title = @"单曲循环";
normalImgName = @"single_cycle";
dkImgName = @"dk_single_cycle";
} else if (mode == SoundPlayModeCycle) {
mode = SoundPlayModeOrder;
title = @"顺序播放";
normalImgName = @"order_play";
dkImgName = @"dk_order_play";
} else if (mode == SoundPlayModeOrder) {
mode = SoundPlayModeSingle;
title = @"单节播放";
normalImgName = @"single_play";
dkImgName = @"dk_single_play";
}
sender.tag = mode;
[sender setTitle:title forState:UIControlStateNormal];
[sender dk_setImage:DKImagePickerWithNames(normalImgName, dkImgName, normalImgName) forState:UIControlStateNormal];
}
#pragma mark - 点击定时器
- (void)didClickTimerAction:(UIButton *)sender {
[self.timingView display];
}
#pragma mark - 滑块事件
- (void)sliderValueChanged:(UISlider *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(didSliderValueChange:)]) {
[self.delegate didSliderValueChange:sender.value];
}
}
- (void)sliderTouchDown:(UISlider *)sender {
_tapGesture.enabled = NO;
if (self.delegate && [self.delegate respondsToSelector:@selector(didSliderTouchBegan:)]) {
[self.delegate didSliderTouchBegan:sender.value];
}
}
- (void)sliderTouchUpInSide:(UISlider *)sender {
_tapGesture.enabled = YES;
if (self.delegate && [self.delegate respondsToSelector:@selector(didSliderTouchEnded:)]) {
[self.delegate didSliderTouchEnded:sender.value];
}
}
#pragma mark - UIGestureRecognizerDelegate
- (void)actionTapGesture:(UITapGestureRecognizer *)sender {
CGPoint touchPoint = [sender locationInView:self.progressV];
float value = (self.progressV.maximumValue - self.progressV.minimumValue) * (touchPoint.x / self.progressV.width);
[self.progressV setValue:value animated:YES];
if (self.delegate && [self.delegate respondsToSelector:@selector(didSliderTouchEnded:)]) {
[self.delegate didSliderTouchEnded:value];
}
}
#pragma mark - 播放控制事件
- (void)playControlAction:(UIButton *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(playControlActionWithItem:)]) {
[self.delegate playControlActionWithItem:sender];
}
}
#pragma mark - public
- (void)setIsPlaying:(BOOL)isPlaying {
_isPlaying = isPlaying;
self.playerBtn.selected = isPlaying;
}
- (SoundPlayMode)mode {
return self.playModeBtn.tag;
}
#pragma mark - others
- (NSString *)formattimewithtimeinterval:(NSTimeInterval)timeinterval {
int minute = 0, hour = 0, secend = timeinterval;
minute = (secend % 3600)/60;
hour = secend / 3600;
secend = secend % 60;
if (hour == 0) {
return [NSString stringWithFormat:@"%02d:%02d", minute, secend];
} else if (minute == 0){
return [NSString stringWithFormat:@"%02d",secend];
} else {
return [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, secend];
}
}
#pragma mark - lazy
- (UIButton *)dismissBtn {
if (!_dismissBtn) {
_dismissBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, kStatusBarHeight + 19, 30, 30)];
[_dismissBtn dk_setImage:DKImagePickerWithNames(@"home_close", @"dk_home_close", @"home_close") forState:UIControlStateNormal];
[_dismissBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
}
return _dismissBtn;
}
- (UIImageView *)audioPicIV {
if (!_audioPicIV) {
_audioPicIV = [UIImageView new];
[_audioPicIV cornerRadius:72.0];
_audioPicIV.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .5, .5);
}
return _audioPicIV;
}
- (UILabel *)audioNameLab {
if (!_audioNameLab) {
_audioNameLab = [UILabel labWithFont:BoldFont(16)];
_audioNameLab.textAlignment = NSTextAlignmentCenter;
_audioNameLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, ColorFromHex(0xE8E9E9), DSWhite);
}
return _audioNameLab;
}
- (UISlider *)progressV {
if (!_progressV) {
_progressV = [UISlider new];
[_progressV cornerRadius:2.0];
_progressV.minimumTrackTintColor = BrandColor;
_progressV.dk_maximumTrackTintColorPicker = DKColorPickerWithColors(ColorFromHex(0xE3E1E1), ColorFromHex(0x131724), DSWhite);
[_progressV setThumbImage:[UIImage imageNamed:@"muse_slider_thumbImage"] forState:UIControlStateNormal];
[_progressV addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[_progressV addTarget:self action:@selector(sliderTouchDown:) forControlEvents:UIControlEventTouchDown];
[_progressV addTarget:self action:@selector(sliderTouchUpInSide:) forControlEvents:UIControlEventTouchUpInside];
// 为UISlider添加点击事件
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTapGesture:)];
_tapGesture.delegate = self;
[_progressV addGestureRecognizer:_tapGesture];
}
return _progressV;
}
- (UILabel *)proLeftLb {
if (!_proLeftLb) {
_proLeftLb = [UILabel labWithFont:SysFont(12)];
_proLeftLb.textAlignment = NSTextAlignmentLeft;
_proLeftLb.dk_textColorPicker = DKColorPickerWithColors(ColorFromHex(0xAAAAAA), ColorFromHex(0x5A6073), DSWhite);
}
return _proLeftLb;
}
- (UILabel *)proRightLb {
if (!_proRightLb) {
_proRightLb = [UILabel labWithFont:SysFont(12)];
_proRightLb.textAlignment = NSTextAlignmentRight;
_proRightLb.dk_textColorPicker = DKColorPickerWithColors(ColorFromHex(0xAAAAAA), ColorFromHex(0x5A6073), DSWhite);
}
return _proRightLb;
}
- (UIButton *)playerBtn {
if (!_playerBtn) {
_playerBtn = [UIButton new];
_playerBtn.tag = 2;
[_playerBtn addTarget:self action:@selector(playControlAction:) forControlEvents:UIControlEventTouchUpInside];
[_playerBtn dk_setBackgroundImage:DKImagePickerWithNames(@"audio_play_icon", @"dk_audio_play_icon", @"dk_audio_play_icon") forState:UIControlStateNormal];
[_playerBtn dk_setBackgroundImage:DKImagePickerWithNames(@"audio_pause", @"dk_audio_pause", @"dk_audio_pause") forState:UIControlStateSelected];
}
return _playerBtn;
}
- (UIButton *)leftPlayBtn {
if (!_leftPlayBtn) {
_leftPlayBtn = [UIButton new];
_leftPlayBtn.tag = 1;
[_leftPlayBtn addTarget:self action:@selector(playControlAction:) forControlEvents:UIControlEventTouchUpInside];
[_leftPlayBtn setBackgroundImage:[UIImage imageNamed:@"previous_icon"] forState:UIControlStateNormal];
}
return _leftPlayBtn;
}
- (UIButton *)rightPlayBtn {
if (!_rightPlayBtn) {
_rightPlayBtn = [UIButton new];
_rightPlayBtn.tag = 3;
[_rightPlayBtn addTarget:self action:@selector(playControlAction:) forControlEvents:UIControlEventTouchUpInside];
[_rightPlayBtn setBackgroundImage:[UIImage imageNamed:@"next_icon"] forState:UIControlStateNormal];
}
return _rightPlayBtn;
}
- (SPButton *)playerListBtn {
if (!_playerListBtn) {
_playerListBtn = [[SPButton alloc] initWithImagePosition:SPButtonImagePositionTop];
_playerListBtn.imageTitleSpace = 2;
[_playerListBtn addTarget:self action:@selector(didClickPlaylistAction:) forControlEvents:UIControlEventTouchUpInside];
[_playerListBtn setTitleColor:SmallTextColor forState:UIControlStateNormal];
_playerListBtn.titleLabel.font = SysFont(12);
[_playerListBtn dk_setImage:DKImagePickerWithNames(@"playlist_icon", @"dk_playlist_icon", @"playlist_icon") forState:UIControlStateNormal];
[_playerListBtn setTitle:@"播放列表" forState:UIControlStateNormal];
}
return _playerListBtn;
}
- (SPButton *)playModeBtn {
if (!_playModeBtn) {
_playModeBtn = [[SPButton alloc] initWithImagePosition:SPButtonImagePositionTop];
_playModeBtn.imageTitleSpace = 2;
[_playModeBtn addTarget:self action:@selector(switchPlayModeAction:) forControlEvents:UIControlEventTouchUpInside];
[_playModeBtn setTitleColor:SmallTextColor forState:UIControlStateNormal];
_playModeBtn.titleLabel.font = SysFont(12);
[_playModeBtn dk_setImage:DKImagePickerWithNames(@"single_play", @"dk_single_play", @"single_play") forState:UIControlStateNormal];
_playModeBtn.tag = SoundPlayModeSingle;
[_playModeBtn setTitle:@"单节播放" forState:UIControlStateNormal];
}
return _playModeBtn;
}
- (SPButton *)timeCloseBtn {
if (!_timeCloseBtn) {
_timeCloseBtn = [[SPButton alloc] initWithImagePosition:SPButtonImagePositionTop];
_timeCloseBtn.imageTitleSpace = 2;
[_timeCloseBtn addTarget:self action:@selector(didClickTimerAction:) forControlEvents:UIControlEventTouchUpInside];
[_timeCloseBtn setTitleColor:SmallTextColor forState:UIControlStateNormal];
_timeCloseBtn.titleLabel.font = SysFont(12);
[_timeCloseBtn dk_setImage:DKImagePickerWithNames(@"time_close_icon", @"dk_time_close_icon", @"time_close_icon") forState:UIControlStateNormal];
[_timeCloseBtn setTitle:@"定时关闭" forState:UIControlStateNormal];
}
return _timeCloseBtn;
}
- (TimingView *)timingView {
if (!_timingView) {
WS(weakSelf);
_timingView = [[TimingView alloc] initWithSureBlock:^(NSInteger index, NSArray * _Nonnull minuteDatas) {
weakSelf.minuteIndex = index;
weakSelf.countTime = 60 * [minuteDatas[index] intValue];
weakSelf.totalTime = 60 * [minuteDatas[index] intValue];
[weakSelf.timer setFireDate:[NSDate date]];
}];
}
return _timingView;
}
- (NSTimer *)timer {
if (!_timer) {
WS(weakSelf);
_timer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
DSLog(@"============");
// 暂停播放
if (weakSelf.countTime == 0) {
[weakSelf.timer setFireDate:[NSDate distantFuture]];
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(timerHasCountDone)]) {
[weakSelf.delegate timerHasCountDone];
}
return;
}
weakSelf.countTime--;
NSString *countDownStr = [weakSelf formattimewithtimeinterval:weakSelf.countTime];
NSString *totalTimeStr = [weakSelf formattimewithtimeinterval:weakSelf.totalTime];
[weakSelf.timeCloseBtn setTitle:[NSString stringWithFormat:@"%@/%@",countDownStr, totalTimeStr] forState:UIControlStateNormal];
}];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
return _timer;
}
@end