此博客解决了我在开发时所遇到的问题
转载与此处:http://www.cnblogs.com/mgen/p/3374987.html
返回目录
1. 关于录音和Audio Session Categories
如果AVAudioRecorder的averagePowerForChannel和peakPowerForChannel方法总是返回-160的话,那么很有可能是当前的Audio Session Categories不允许进行音频输入(也就是麦克风输入)。如:AVAudioSessionCategorySoloAmbient/kAudioSessionCategory_SoloAmbientSound,或者AVAudioSessionCategoryPlayback/kAudioSessionCategory_MediaPlayback。
如果这样的话,我们需要把当前Audio Session Categories设置成AVAudioSessionCategoryRecord/kAudioSessionCategory_RecordAudio,或者AVAudioSessionCategoryPlayAndRecord/kAudioSessionCategory_PlayAndRecord。
可以使用两套API,一种是AVFoundation Framework中的API。如下:
NSError*setCategoryError =nil;
BOOLsuccess = [[AVAudioSessionsharedInstance]
setCategory:AVAudioSessionCategoryRecord
//或者AVAudioSessionCategoryPlayAndRecord
error:
&setCategoryError];
另一种是使用AudioToolbox Framework,它是基于C的API,如下:
//或者使用kAudioSessionCategory_PlayAndRecord
UInt32sessionCategory =kAudioSessionCategory_RecordAudio;
OSStatusresult =AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory),
&sessionCategory);
返回目录
2. 分贝数据的处理
根据Apple文档,AVAudioRecorder的averagePowerForChannel和peakPowerForChannel方法返回的是分贝数据,数值在-160
- 0之间(可能会返回大于0的值如果超出了极限)。在实际测试中,比如我在办公室(不算吵也不算特别安静的环境下)我测试averagePowerForChannel的返回值平均在-35左右徘徊。
有很多方法可以把这个原始的分贝数据转化成更可读或者更可用的形式。如Apple SpeakHere Sample。
或者自己手动设置一个分贝的范围,然后根据比例输出自己需要的分贝范围:比如下段代码:
//用于监控AVAudioRecorder数据的Timer回调方法。
//注意设置AVAudioRecorder的meteringEnabled属性为YES。
//recorder变量是AVAudioRecorder对象。
- (void)timerCallback:(NSTimer*)timer
{
[recorderupdateMeters];
//averagePowerForChannel调用结果
floatavg
= [recorderaveragePowerForChannel:0];
//比如把-60作为最低分贝
floatminValue
= -60;
//把60作为获取分配的范围
floatrange
=60;
//把100作为输出分贝范围
floatoutRange
=100;
//确保在最小值范围内
if(avg
< minValue)
{
avg = minValue;
}
//计算显示分贝
floatdecibels
= (avg + range) / range * outRange;
NSLog(@"%f",
decibels);
}
在办公室下分贝大约在40左右。
还有这种方法,觉得他更符合现实的分贝数据,代码:
//用于监控AVAudioRecorder数据的Timer回调方法。
//注意设置AVAudioRecorder的meteringEnabled属性为YES。
//recorder变量是AVAudioRecorder对象。
//http://stackoverflow.com/questions/9247255/am-i-doing-the-right-thing-to-convert-decibel-from-120-0-to-0-120/16192481#16192481
- (void)levelTimerCallback:(NSTimer*)timer
{
[recorderupdateMeters];
float
level;// The linear 0.0 .. 1.0 value we need.
float
minDecibels = -80.0f;// Or use -60dB, which I measured
in a silent room.
float
decibels = [recorderaveragePowerForChannel:0];
if(decibels
< minDecibels)
{
level =0.0f;
}
elseif(decibels
>=0.0f)
{
level =1.0f;
}
else
{
float
root =2.0f;
float
minAmp =powf(10.0f,0.05f*
minDecibels);
float
inverseAmpRange =1.0f/ (1.0f-
minAmp);
float
amp =powf(10.0f,0.05f*
decibels);
float
adjAmp = (amp - minAmp) * inverseAmpRange;
level =powf(adjAmp,1.0f/
root);
}
NSLog(@"平均值%f",
level *120);
}
返回目录
3. iOS 7中默认配置的变化
在iOS 6中,AVAudioRecorder的默认配置(通过其settings属性)是:
{
AVFormatIDKey = 1819304813;
AVLinearPCMBitDepthKey = 16;
AVLinearPCMIsBigEndianKey = 0;
AVLinearPCMIsFloatKey = 0;
AVLinearPCMIsNonInterleaved = 0;
AVNumberOfChannelsKey = 1;
AVSampleRateKey = 44100;
}
而在iOS 7中,默认配置是:
{
AVFormatIDKey = 1819304813;
AVLinearPCMBitDepthKey = 16;
AVLinearPCMIsBigEndianKey = 0;
AVLinearPCMIsFloatKey = 0;
AVLinearPCMIsNonInterleaved = 0;
AVNumberOfChannelsKey = 2;
AVSampleRateKey = 44100;
}
变化是AVNumberOfChannelsKey从1变成了2,也就是支持两个音道的录制,显然一个麦克风不需要,最好把AVNumberOfChannelsKey设置成1。
关于AVAudioRecorder的配置项,可以参考这个帖子。
分享到:
相关推荐
我在git上也上传了一份https://github.com/yuanjunxiao/ios-AVAudioRecorder-.git
AVAudioRecorder 的使用演示
96. Recording Audio on iOS 10 with AVAudioRecorder 97. An iOS 10 Speech Recognition Tutorial 98. An iOS 10 Real-Time Speech Recognition Tutorial 99. An Introduction to SiriKit 100. An iOS 10 Example ...
1 使用AVAudioRecorder录音成wav文件 2 为了避免录音启动耗时0 5秒(因为我录音时会有个mic图片的gif 发现显示gif显示被延时0 5秒) 所以 开启子线程做录音的operation 3 为了让可恶的android也能使用 需要将wav >...
-- IOS利用AVFoundation框架实现录音和播放 (AVAudioSession AVAudioRecorder AVAudioPlayer) -- IOS之分析网易新闻存储数据 CoreData的使用 增删改查 -- IOS二维码扫描Demo -- 18个 ios 项目源代码 -- iOS通讯录...
添加AVFoundation Framework 为使用AVAudioRecorder类,我们需要向项目添加AVFoundation framework: 在项目Groups & Files面板上展开Targets 按Control-点击或右击MicBlow 选择Add > Existing Frameworks… 按下...
利用AVAudioRecorder录音 利用AVAudioPlayer播放 AVAudioSession录音权限 简单的录音播放demo,类似微信的按下录音,松开录音完成,并播放。
Recording Audio on iOS 8 with AVAudioRecorder Chapter 90. Integrating Twitter and Facebook into iOS 8 Applications Chapter 91. An iOS 8 Facebook Integration Tutorial using UIActivityViewController ...
利用AVAudioRecorder录音 利用AVAudioPlayer播放 AVAudioSession录音权限 简单的录音播放demo,类似微信的按下录音,松开录音完成,并播放。
Recording Audio on iOS 9 with AVAudioRecorder Chapter 96. Integrating Twitter and Facebook into iOS 9 Applications Chapter 97. An iOS 9 Social Media Integration Tutorial using ...
iOS AVFoundation框架下的AVAudioRecorder录音机的实现。
YZSimpleAudioKit 一些用于 AVAudioPlayer 和 AVAudioRecorder 的“包装类”或自定义类。
二、使用AVAudioRecorder录音; 三、使用AVAudioPlayer播放,并添加播放动画; 四、使用lame将caf音频转化为mp3; 五、将mp3 转化为 base64编码; 六、查看录音文件大小; 七、删除语音文件;
音频的录制与播放,进行封装后的单例工具组件。 (1)使用AVAudioRecorder进行录音 (2)使用AVAudioPlayer进行音频播放 SYAudio:https://github.com/potato512/SYAudio
上一篇中总结了iOS中音效和音频播放的最基本使用方法,其中音频的播放控制是使用AVFoundation.framework框架中的AVAudioPlayer播放器对象来实现的,而这里音频的录制则是使用了同样框架下的一个叫AVAudioRecorder的...
在AVFoundation框架中有一个AVAudioRecorder类专门处理录音操作,它同样支持多种音频格式。与AVAudioPlayer类似,你完全可以将它看成是一个录音机控制类,下面是常用的属性和方法: 先来了解下AVAudioRecorder的常用...
AVAudioRecorder *audioRecorder; } @end 在ViewDidLoad中: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); ; button.b
背景 因为我的公司需要设计到app与硬件的通信,所以去年深入...由于App是通过AVAudioRecorder录制音频,默认格式为pcm,文件比较大,所以不适合用于聊天通信的文件格式,所以最优的选择是转换成amr格式 音频格式转换方式
播放本地音频、AVAudioRecorder和Audio Queue录制音频、拍照等功能
作者potato512,源码SYAudio,音频的录制与播放,进行封装后的单例工具组件。(1)使用AVAudioRecorder进行录音(2)使用AVAudioPlayer进行音频播放