- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章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 。
最后此篇关于iOS使用音频处理框架The Amazing Audio Engine实现音频录制播放的文章就讲到这里了,如果你想了解更多关于iOS使用音频处理框架The Amazing Audio Engine实现音频录制播放的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
对于 Metal ,如果对主纹理进行 mipmap 处理,是否还需要对多采样纹理进行 mipmap 处理?我阅读了苹果文档,但没有得到任何相关信息。 最佳答案 Mipmapping 适用于您将从中
我正在使用的代码在后端 Groovy 代码中具有呈现 GSP(Groovy 服务器页面)的 Controller 。对于前端,我们使用 React-router v4 来处理路由。我遇到的问题是,通过
我们正在 build 一个巨大的网站。我们正在考虑是在服务器端(ASP .Net)还是在客户端进行 HTML 处理。 例如,我们有 HTML 文件,其作用类似于用于生成选项卡的模板。服务器端获取 HT
我正在尝试将图像加载到 void setup() 中的数组中,但是当我这样做时出现此错误:“类型不匹配,'processing .core.PImage' does not匹配“processing.
我正在尝试使用其私有(private)应用程序更新 Shopify 上的客户标签。我用 postman 尝试过,一切正常,但通过 AJAX,它带我成功回调而不是错误,但成功后我得到了身份验证链接,而不
如何更改我的 Processing appIconTest.exe 导出的默认图标在窗口中的应用程序? 默认一个: 最佳答案 经过一些研究,我能找到的最简单的解决方案是: 进入 ...\process
我在 Processing 中做了一个简单的小游戏,但需要一些帮助。我有一个 mp3,想将它添加到我的应用程序中,以便在后台循环运行。 这可能吗?非常感谢。 最佳答案 您可以使用声音库。处理已经自带
我有几个这样创建的按钮: 在 setup() PImage[] imgs1 = {loadImage("AREA1_1.png"),loadImage("AREA1_2.png"),loadImage
我正在尝试使用 Processing 创建一个多人游戏,但无法弄清楚如何将屏幕分成两个以显示玩家的不同情况? 就像在 c# 中一样,我们有Viewport leftViewport,rightView
我一直在尝试使用 Moore 邻域在处理过程中创建元胞自动机,到目前为止非常成功。我已经设法使基本系统正常工作,现在我希望通过添加不同的功能来使用它。现在,我检查细胞是否存活。如果是,我使用 fill
有没有办法用 JavaScript 代码检查资源使用情况?我可以检查脚本的 RAM 使用情况和 CPU 使用情况吗? 由于做某事有多种方法,我可能会使用不同的方法编写代码,并将其保存为两个不同的文件,
我想弄清楚如何处理这样的列表: [ [[4,6,7], [1,2,4,6]] , [[10,4,2,4], [1]] ] 这是一个整数列表的列表 我希望我的函数将此列表作为输入并返回列表中没有重复的整
有没有办法在不需要时处理 MethodChannel/EventChannel ?我问是因为我想为对象创建多个方法/事件 channel 。 例子: class Call { ... fields
我有一个关于在 Python3 中处理 ConnectionResetError 的问题。这通常发生在我使用 urllib.request.Request 函数时。我想知道如果我们遇到这样的错误是否可
我一直在努力解决这个问题几个小时,但无济于事。代码很简单,一个弹跳球(粒子)。将粒子的速度初始化为 (0, 0) 将使其保持上下弹跳。将粒子的初始化速度更改为 (0, 0.01) 或任何十进制浮点数都
我把自己弄得一团糟。 我想在我的系统中添加 python3.6 所以我决定在我的 Ubuntu 19.10 中卸载现有的。但是现在每次我想安装一些东西我都会得到这样的错误: dpkg: error w
我正在努力解决 Rpart 包中的 NA 功能。我得到了以下数据框(下面的代码) Outcome VarA VarB 1 1 1 0 2 1 1 1
我将 Java 与 JSF 一起使用,这是 Glassfish 3 容器。 在我的 Web 应用程序中,我试图实现一个文件(图像)管理系统。 我有一个 config.properties我从中读取上传
所以我一直在Processing工作几个星期以来,虽然我没有编程经验,但我已经转向更复杂的项目。我正在编写一个进化模拟器,它会产生具有随机属性的生物。 最终,我将添加复制,但现在这些生物只是在屏幕上漂
有人知道 Delphi 2009 对“with”的处理有什么不同吗? 我昨天解决了一个问题,只是将“with”解构为完整引用,如“with Datamodule、Dataset、MainForm”。
我是一名优秀的程序员,十分优秀!