服务器之家

服务器之家 > 正文

iOS使用音频处理框架The Amazing Audio Engine实现音频录制播放

时间:2021-05-24 16:02     来源/作者:hero_wqb

ios 第三方音频框架the amazing audio engine使用,实现音频录制、播放,可设置配乐。

首先看一下效果图:

iOS使用音频处理框架The Amazing Audio Engine实现音频录制播放

下面贴上核心控制器代码:

?
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
#import "viewcontroller.h"
#import <avfoundation/avfoundation.h>
#import "hwprogresshud.h"
#import "uiimage+hw.h"
#import "aerecorder.h"
#import "hwrecordingdrawview.h"
 
#define kmainw [uiscreen mainscreen].bounds.size.width
#define kmainh [uiscreen mainscreen].bounds.size.height
 
@interface viewcontroller ()
 
@property (nonatomic, strong) aerecorder *recorder;
@property (nonatomic, strong) aeaudiocontroller *audiocontroller;
@property (nonatomic, strong) aeaudiofileplayer *player;
@property (nonatomic, strong) aeaudiofileplayer *backgroundplayer;
@property (nonatomic, strong) nstimer *timer;
@property (nonatomic, strong) nsmutablearray *soundsource;
@property (nonatomic, weak) hwrecordingdrawview *recordingdrawview;
@property (nonatomic, weak) uilabel *reclabel;
@property (nonatomic, weak) uilabel *recordtimelabel;
@property (nonatomic, weak) uilabel *playtimelabel;
@property (nonatomic, weak) uibutton *auditionbtn;
@property (nonatomic, weak) uibutton *recordbtn;
@property (nonatomic, weak) uislider *slider;
@property (nonatomic, copy) nsstring *path;
 
@end
 
@implementation viewcontroller
 
- (aeaudiocontroller *)audiocontroller
{
 if (!_audiocontroller) {
 _audiocontroller = [[aeaudiocontroller alloc] initwithaudiodescription:[aeaudiocontroller noninterleavedfloatstereoaudiodescription] inputenabled:yes];
 _audiocontroller.preferredbufferduration = 0.005;
 _audiocontroller.usemeasurementmode = yes;
 }
 
 return _audiocontroller;
}
 
- (nsmutablearray *)soundsource
{
 if (!_soundsource) {
 _soundsource = [nsmutablearray array];
 }
 
 return _soundsource;
}
 
- (void)viewdidload {
 [super viewdidload];
 
 [self creatcontrol];
}
 
- (void)creatcontrol
{
 cgfloat marginx = 30.0f;
 
 //音频视图
 hwrecordingdrawview *recordingdrawview = [[hwrecordingdrawview alloc] initwithframe:cgrectmake(marginx, 80, kmainw - marginx * 2, 100)];
 [self.view addsubview:recordingdrawview];
 _recordingdrawview = recordingdrawview;
 
 //rec
 uilabel *reclabel = [[uilabel alloc] initwithframe:cgrectmake(marginx, cgrectgetmaxy(recordingdrawview.frame) + 20, 80, 40)];
 reclabel.text = @"rec";
 reclabel.textcolor = [uicolor redcolor];
 [self.view addsubview:reclabel];
 _reclabel = reclabel;
 
 //录制时间
 uilabel *recordtimelabel = [[uilabel alloc] initwithframe:cgrectmake(cgrectgetmaxx(reclabel.frame) + 20, cgrectgetminy(reclabel.frame), 150, 40)];
 recordtimelabel.text = @"录制时长:00:00";
 [self.view addsubview:recordtimelabel];
 _recordtimelabel = recordtimelabel;
 
 //播放时间
 uilabel *playtimelabel = [[uilabel alloc] initwithframe:cgrectmake(cgrectgetminx(recordtimelabel.frame), cgrectgetmaxy(recordtimelabel.frame), 150, 40)];
 playtimelabel.text = @"播放时长:00:00";
 playtimelabel.hidden = yes;
 [self.view addsubview:playtimelabel];
 _playtimelabel = playtimelabel;
 
 //配乐按钮
 nsarray *titlearray = @[@"无配乐", @"夏天", @"阳光海湾"];
 cgfloat btnw = 80.0f;
 cgfloat padding = (kmainw - marginx * 2 - btnw * titlearray.count) / (titlearray.count - 1);
 for (int i = 0; i < titlearray.count; i++) {
 cgfloat btnx = marginx + (btnw + padding) * i;
 uibutton *btn = [[uibutton alloc] initwithframe:cgrectmake(btnx, cgrectgetmaxy(playtimelabel.frame) + 20, btnw, btnw)];
 [btn settitle:titlearray[i] forstate:uicontrolstatenormal];
 btn.layer.cornerradius = btnw * 0.5;
 btn.layer.maskstobounds = yes;
 [btn setbackgroundimage:[uiimage imagewithcolor:[uicolor graycolor]] forstate:uicontrolstatenormal];
 [btn setbackgroundimage:[uiimage imagewithcolor:[uicolor orangecolor]] forstate:uicontrolstateselected];
 if (i == 0) btn.selected = yes;
 btn.tag = 100 + i;
 [btn addtarget:self action:@selector(changebackgroundmusic:) forcontrolevents:uicontroleventtouchupinside];
 [self.view addsubview:btn];
 }
 
 //配乐音量
 uilabel *backgroundlabel = [[uilabel alloc] initwithframe:cgrectmake(marginx + 10, cgrectgetmaxy(playtimelabel.frame) + 120, 80, 40)];
 backgroundlabel.text = @"配乐音量";
 [self.view addsubview:backgroundlabel];
 
 //配乐音量
 uislider *slider = [[uislider alloc] initwithframe:cgrectmake(cgrectgetmaxx(backgroundlabel.frame) + 10, cgrectgetminy(backgroundlabel.frame), 210, 40)];
 slider.value = 0.4f;
 [slider addtarget:self action:@selector(slidervaluechanged:) forcontrolevents:uicontroleventvaluechanged];
 [self.view addsubview:slider];
 _slider = slider;
 
 //试听按钮
 uibutton *auditionbtn = [[uibutton alloc] initwithframe:cgrectmake(marginx, kmainh - 150, 120, 80)];
 auditionbtn.hidden = yes;
 auditionbtn.backgroundcolor = [uicolor blackcolor];
 [auditionbtn settitle:@"试听" forstate:uicontrolstatenormal];
 [auditionbtn settitle:@"停止" forstate:uicontrolstateselected];
 [auditionbtn addtarget:self action:@selector(auditionbtnonclick:) forcontrolevents:uicontroleventtouchupinside];
 [self.view addsubview:auditionbtn];
 _auditionbtn = auditionbtn;
 
 //录音按钮
 uibutton *recordbtn = [[uibutton alloc] initwithframe:cgrectmake(kmainw - marginx - 120, kmainh - 150, 120, 80)];
 recordbtn.backgroundcolor = [uicolor blackcolor];
 [recordbtn settitle:@"开始" forstate:uicontrolstatenormal];
 [recordbtn settitle:@"暂停" forstate:uicontrolstateselected];
 [recordbtn addtarget:self action:@selector(recordbtnonclick:) forcontrolevents:uicontroleventtouchupinside];
 [self.view addsubview:recordbtn];
 _recordbtn = recordbtn;
}
 
//配乐按钮点击事件
- (void)changebackgroundmusic:(uibutton *)btn
{
 //更新选中状态
 for (int i = 0; i < 3; i++) {
 uibutton *button = (uibutton *)[self.view viewwithtag:100 + i];
 button.selected = no;
 }
 btn.selected = yes;
 
 //移除之前配乐
 if (_backgroundplayer) {
 [_audiocontroller removechannels:@[_backgroundplayer]];
 _backgroundplayer = nil;
 }
 
 nsurl *url;
 if (btn.tag == 100) {
 return;
 }else if (btn.tag == 101) {
 url = [[nsbundle mainbundle]urlforresource:@"夏天.mp3" withextension:nil];
 }else if (btn.tag == 102) {
 url = [[nsbundle mainbundle]urlforresource:@"阳光海湾.mp3" withextension:nil];
 }
 [self.audiocontroller start:null];
 
 nserror *averror = null;
 _backgroundplayer = [aeaudiofileplayer audiofileplayerwithurl:url error:&averror];
 _backgroundplayer.volume = _slider.value;
 _backgroundplayer.loop = yes;
 if (!_backgroundplayer) {
 [[[uialertview alloc] initwithtitle:@"error"
     message:[nsstring stringwithformat:@"couldn't start playback: %@", [averror localizeddescription]]
     delegate:nil
    cancelbuttontitle:nil
    otherbuttontitles:@"ok", nil] show];
 return;
 }
 
 //放完移除
 _backgroundplayer.removeuponfinish = yes;
 __weak viewcontroller *weakself = self;
 _backgroundplayer.completionblock = ^{
 weakself.backgroundplayer = nil;
 };
 [_audiocontroller addchannels:@[_backgroundplayer]];
}
 
//配乐音量slider滑动事件
- (void)slidervaluechanged:(uislider *)slider
{
 if (_backgroundplayer) _backgroundplayer.volume = slider.value;
}
 
//录音按钮点击事件
- (void)recordbtnonclick:(uibutton *)btn
{
 [[avaudiosession sharedinstance] requestrecordpermission:^(bool granted) {
 if (granted) {
  //用户同意获取麦克风
  dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(.1f * nsec_per_sec)), dispatch_get_main_queue(), ^{
  btn.selected = !btn.selected;
  
  if (btn.selected) {
   [self startrecord];
  }else {
   [self finishrecord];
  }
  });
  
 }else {
  //用户不同意获取麦克风
  [hwprogresshud showmessage:@"需要访问您的麦克风,请在“设置-隐私-麦克风”中允许访问。" duration:3.f];
 }
 }];
}
 
//开始录音
- (void)startrecord
{
 _auditionbtn.hidden = yes;
 [self.audiocontroller start:null];
 _recorder = [[aerecorder alloc] initwithaudiocontroller:_audiocontroller];
 _path = [self getpath];
 
 nserror *error = null;
 if ( ![_recorder beginrecordingtofileatpath:_path filetype:kaudiofilem4atype error:&error] ) {
 [[[uialertview alloc] initwithtitle:@"error" message:[nsstring stringwithformat:@"couldn't start recording: %@", [error localizeddescription]] delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil] show];
 _recorder = nil;
 return;
 }
 
 [self.soundsource removeallobjects];
 [self removetimer];
 [self addrecordtimer];
 
 [_audiocontroller addoutputreceiver:_recorder];
 [_audiocontroller addinputreceiver:_recorder];
}
 
//结束录音
- (void)finishrecord
{
 _auditionbtn.hidden = no;
 _reclabel.hidden = no;
 [self removetimer];
 
 [_recorder finishrecording];
 [_audiocontroller removeoutputreceiver:_recorder];
 [_audiocontroller removeinputreceiver:_recorder];
 _recorder = nil;
}
 
//添加录音定时器
- (void)addrecordtimer
{
 self.timer = [nstimer scheduledtimerwithtimeinterval:.2f target:self selector:@selector(recordtimeraction) userinfo:nil repeats:yes];
 [[nsrunloop mainrunloop] addtimer:self.timer formode:nsrunloopcommonmodes];
}
 
//录音定时器事件
- (void)recordtimeraction
{
 //获取音频
 [catransaction begin];
 [catransaction setdisableactions:yes];
 float32 inputavg, inputpeak, outputavg, outputpeak;
 [_audiocontroller inputaveragepowerlevel:&inputavg peakholdlevel:&inputpeak];
 [_audiocontroller outputaveragepowerlevel:&outputavg peakholdlevel:&outputpeak];
 [self.soundsource insertobject:[nsnumber numberwithfloat:(inputpeak + 18) * 2.8] atindex:0];
 [catransaction commit];
 _recordingdrawview.pointarray = _soundsource;
 
 //rec闪动
 _reclabel.hidden = (int)[self.recorder currenttime] % 2 == 1 ? yes : no;
 
 //录音时间
 nsstring *str = [self strwithtime:[self.recorder currenttime] interval:0.5f];
 if ([str intvalue] < 0) str = @"录制时长:00:00";
 [self.recordtimelabel settext:[nsstring stringwithformat:@"录制时长:%@", str]];
}
 
//移除定时器
- (void)removetimer
{
 [self.timer invalidate];
 self.timer = nil;
}
 
//试听按钮点击事件
- (void)auditionbtnonclick:(uibutton *)btn
{
 btn.selected = !btn.selected;
 
 if (btn.selected) {
 [self playrecord];
 }else {
 [self stopplayrecord];
 }
}
 
//播放录音
- (void)playrecord
{
 //更新界面
 _recordbtn.hidden = yes;
 [_playtimelabel settext:@"播放时长:00:00"];
 _playtimelabel.hidden = no;
 
 //取消背景音乐
 [self changebackgroundmusic:(uibutton *)[self.view viewwithtag:100]];
 
 if (![[nsfilemanager defaultmanager] fileexistsatpath:_path]) return;
 
 nserror *error = nil;
 _player = [aeaudiofileplayer audiofileplayerwithurl:[nsurl fileurlwithpath:_path] error:&error];
 if (!_player) {
 [[[uialertview alloc] initwithtitle:@"error"
     message:[nsstring stringwithformat:@"couldn't start playback: %@", [error localizeddescription]]
     delegate:nil
    cancelbuttontitle:nil
    otherbuttontitles:@"ok", nil] show];
 return;
 }
 
 [self addplaytimer];
 _player.removeuponfinish = yes;
 
 __weak viewcontroller *weakself = self;
 _player.completionblock = ^{
 weakself.player = nil;
 weakself.auditionbtn.selected = no;
 [weakself stopplayrecord];
 };
 [self.audiocontroller start:null];
 [self.audiocontroller addchannels:@[_player]];
}
 
//停止播放录音
- (void)stopplayrecord
{
 _recordbtn.hidden = no;
 _playtimelabel.hidden = yes;
 [self removetimer];
 if (_player) [_audiocontroller removechannels:@[_player]];
}
 
//添加播放定时器
- (void)addplaytimer
{
 self.timer = [nstimer scheduledtimerwithtimeinterval:1.0f target:self selector:@selector(playtimeraction) userinfo:nil repeats:yes];
 [[nsrunloop mainrunloop] addtimer:self.timer formode:nsrunloopcommonmodes];
}
 
//播放定时器事件
- (void)playtimeraction
{
 //播放时间
 nsstring *str = [self strwithtime:[_player currenttime] interval:1.f];
 if ([str intvalue] < 0) str = @"播放时长:00:00";
 [_playtimelabel settext:[nsstring stringwithformat:@"播放时长:%@", str]];
}
 
//录制音频沙盒路径
- (nsstring *)getpath
{
 nsdateformatter *formatter = [[nsdateformatter alloc] init];
 [formatter setdateformat:@"yyyymmddhhmmss"];
 nsstring *recordname = [nsstring stringwithformat:@"%@.wav", [formatter stringfromdate:[nsdate date]]];
 nsstring *path = [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject] stringbyappendingpathcomponent:recordname];
 
 return path;
}
 
//时长长度转时间字符串
- (nsstring *)strwithtime:(double)time interval:(cgfloat)interval
{
 int minute = (time * interval) / 60;
 int second = (int)(time * interval) % 60;
 
 return [nsstring stringwithformat:@"%02d:%02d", minute, second];
}
 
@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/hero_wqb/article/details/76460663

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部