DWAudioPlayView.m 9.55 KB
Newer Older
Han Ruisong's avatar
Han Ruisong committed
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
//
//  DWAudioPlayView.m
//  Demo
//
//  Created by luyang on 2017/11/9.
//  Copyright © 2017年 com.bokecc.www. All rights reserved.
//

#import "DWAudioPlayView.h"


@interface DWAudioPlayView()

//滑动条是否滑动
@property (nonatomic,assign)BOOL isAudioSlidering;

@end

@implementation DWAudioPlayView

- (instancetype )initWithFrame:(CGRect)frame{
   
    self =[super initWithFrame:frame];
    if (self) {
        
        [self loadSubviews];
    }
    
    return self;
}

- (void)loadSubviews{
    
    _imageView =[[UIImageView alloc]init];
    _imageView.image =[UIImage imageNamed:@"bgImage"];
    [self addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    _backBtn =[self creatButtonWithImageName:@"player-back-button.png" selector:@selector(audioBackAction:)];
    [self addSubview:_backBtn];
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.height.width.mas_equalTo(25);
        make.left.mas_equalTo(self.mas_left).offset(12);
        make.top.mas_equalTo(self.mas_top).offset(7);
        
        
    }];
    
    _titleLabel =[[UILabel alloc]init];
    _titleLabel.text =@"我是音频";
    _titleLabel.font =[UIFont systemFontOfSize:13];
    _titleLabel.textColor =[UIColor whiteColor];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(_backBtn.mas_right);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(512/2);
        make.centerY.mas_equalTo(_backBtn);
    }];
    
    
    _downloadBtn =[self creatButtonWithImageName:@"download_ic" selector:@selector(audioDownloadAction:)];
    [self addSubview:_downloadBtn];
    [_downloadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.width.mas_equalTo(30);
        make.centerY.mas_equalTo(_backBtn);
        make.right.mas_equalTo(self.mas_right).offset(-12);
        
        
    }];
    
    _rateBtn =[self creatButtonWithImageName:@"" selector:@selector(audioRateAction:)];
    _rateBtn.tag =101;
    _rateBtn.layer.cornerRadius =2;
    _rateBtn.layer.masksToBounds =YES;
    [self addSubview:_rateBtn];
    [_rateBtn setTitle:@"语速X1.0" forState:UIControlStateNormal];
    _rateBtn.titleLabel.font =[UIFont systemFontOfSize:12];
    [_rateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(124/2);
        make.height.mas_equalTo(50/2);
        make.top.mas_equalTo(self.mas_top).offset(134/2);
        make.centerX.mas_equalTo(self);
        
        
    }];
    
    _progressView =[[UIProgressView alloc]initWithFrame:CGRectMake((ScreenWidth -620/2)/2,self.center.y, 620/2, 30)];
    _progressView.progressTintColor =[DWTools colorWithHexString:@"#999999"];
    _progressView.trackTintColor =[UIColor whiteColor];
    [self addSubview:_progressView];
    
    _audioSlider =[[UISlider alloc]init];
    [self addSubview:_audioSlider];
    _audioSlider.minimumValue = 0.0f;
    _audioSlider.maximumValue = 1.0f;
    _audioSlider.value = 0.0f;
    _audioSlider.continuous = NO;

    [_audioSlider setMaximumTrackTintColor:[UIColor clearColor]];
    [_audioSlider setMinimumTrackTintColor:[DWTools colorWithHexString:@"#ff6633"]];
    [ _audioSlider setThumbImage:[UIImage imageNamed:@"player-slider-handle"]
                              forState:UIControlStateNormal];
    
    [_audioSlider addTarget:self action:@selector(durationSliderMoving:) forControlEvents:UIControlEventValueChanged | UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
  
   [ _audioSlider addTarget:self action:@selector(durationSliderEnded:) forControlEvents:   UIControlEventTouchCancel];

    [ _audioSlider addTarget:self action:@selector(durationSliderBegan:) forControlEvents:UIControlEventTouchDown];
    
    
    [_audioSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(620/2);
        make.height.mas_equalTo(30);
        make.center.mas_equalTo(self);
        
    }];
    
   
   
    
    _playLabel =[[UILabel alloc]init];
    _playLabel.font =[UIFont systemFontOfSize:11];
    _playLabel.textColor =[UIColor whiteColor];
    _playLabel.text =@"00:00:00";
    [self addSubview:_playLabel];
    [_playLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_audioSlider.mas_left);
        make.width.mas_equalTo(76);
        make.height.mas_equalTo(12);
        make.top.mas_equalTo(_audioSlider.mas_bottom).offset(25/2);
        
        
    }];
    
    _totalLabel =[[UILabel alloc]init];
    _totalLabel.font =[UIFont systemFontOfSize:11];
    _totalLabel.text =@"00:00:00";
    _totalLabel.textColor =[UIColor whiteColor];
    _totalLabel.textAlignment =NSTextAlignmentRight;
    [self addSubview:_totalLabel];
    [_totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.width.top.mas_equalTo(_playLabel);
        make.right.mas_equalTo(_audioSlider.mas_right);
        
    }];
    
    _playBtn =[self creatButtonWithImageName:@"play" selector:@selector(playBtnAction:)];
    [_playBtn setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateSelected];
    _playBtn.layer.cornerRadius =89/2/2;
    _playBtn.layer.masksToBounds =YES;
    [self addSubview:_playBtn];
    [_playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self);
        make.height.width.mas_equalTo(89/2);
        make.top.mas_equalTo(_playLabel.mas_bottom).offset(30/2);
        
    }];
    
    _leftView =[[UIView alloc]init];
    _leftView.backgroundColor =[DWTools colorWithHexString:@"#ffffff"];
    _leftView.alpha =0.3;
    [self addSubview:_leftView];
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(58/2);
        make.width.mas_equalTo(3/2);
        make.centerY.mas_equalTo(_playBtn);
        make.right.mas_equalTo(_playBtn.mas_left).offset(-70/2);
        
        
    }];
    
    
    _rightView =[[UIView alloc]init];
    _rightView.backgroundColor =[DWTools colorWithHexString:@"#ffffff"];
    _rightView.alpha =0.3;
    [self addSubview:_rightView];
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(58/2);
        make.width.mas_equalTo(3/2);
        make.centerY.mas_equalTo(_playBtn);
        make.left.mas_equalTo(_playBtn.mas_right).offset(70/2);
        
        
    }];
    
    //回退15s
    _fallbackBtn =[self creatButtonWithImageName:@"fallback" selector:@selector(fallbackBtnAction:)];
    _fallbackBtn.layer.cornerRadius =1.5;
    _fallbackBtn.layer.masksToBounds =YES;
    [self addSubview:_fallbackBtn];
    [_fallbackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(53/2);
        make.width.mas_equalTo(71/2);
        make.centerY.mas_equalTo(_playBtn);
        make.right.mas_equalTo(_leftView.mas_left).offset(-65/2);
        
    }];
    
    //前进15s
    _farwardBtn =[self creatButtonWithImageName:@"farward" selector:@selector(farwardBtnAction:)];
    _farwardBtn.layer.cornerRadius =1.5;
    _farwardBtn.layer.masksToBounds =YES;
    [self addSubview:_farwardBtn];
    [_farwardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(53/2);
        make.width.mas_equalTo(71/2);
        make.centerY.mas_equalTo(_playBtn);
        make.left.mas_equalTo(_rightView.mas_right).offset(65/2);
        
    }];
    
    
}

//关闭
- (void)audioBackAction:(UIButton *)sender{
    
    if (_delegate && [_delegate respondsToSelector:@selector(audioPlayViewBackAction)]) {
        
        [_delegate audioPlayViewBackAction];
    }
    
}

- (void)audioDownloadAction:(UIButton *)sender{
    
    if (_delegate && [_delegate respondsToSelector:@selector(audioPlayViewDownloadAction)]) {
        
        [_delegate audioPlayViewDownloadAction];
    }

    
    
}

- (void)audioRateAction:(UIButton *)sender{
    
    if (_delegate && [_delegate respondsToSelector:@selector(audioPlayViewRateAction)]) {
        
        [_delegate audioPlayViewRateAction];
    }

    
    
}

- (void)durationSliderMoving:(UISlider *)slider{
    
    self.isAudioSlidering =NO;
    if (_delegate && [_delegate respondsToSelector:@selector(durationPlayViewSliderMoving)]) {
        
        [_delegate durationPlayViewSliderMoving];
    }
    
   
    
    
}

- (void)durationSliderEnded:(UISlider *)slider{
    
   self.isAudioSlidering =NO;
    
}

- (void)durationSliderBegan:(UISlider *)slider{
    
   self.isAudioSlidering =YES;
}

- (void)playBtnAction:(UIButton *)sender{
    
    if (_delegate && [_delegate respondsToSelector:@selector(playPlayViewBtnAction)]) {
        
        [_delegate playPlayViewBtnAction];
    }
    
    
}

//回退15s
- (void)fallbackBtnAction:(UIButton *)sender{
    
    
    if (_delegate && [_delegate respondsToSelector:@selector(fallbackPlayViewBtnAction)]) {
        
        [_delegate fallbackPlayViewBtnAction];
    }
    
    
}

//前进15s
- (void)farwardBtnAction:(UIButton *)sender{
    
    if (_delegate && [_delegate respondsToSelector:@selector(farwardPlayViewBtnAction)]) {
        
        [_delegate farwardPlayViewBtnAction];
    }
    
}


- (UIButton *)creatButtonWithImageName:(NSString *)imageName selector:(SEL )selector{
    
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    
    return btn;
    
}

@end