`
252190908
  • 浏览: 228419 次
文章分类
社区版块
存档分类
最新评论

iOS: 使用AVAudioRecorder中遇到的问题

 
阅读更多

此博客解决了我在开发时所遇到的问题

转载与此处: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回调方法。

//注意设置AVAudioRecordermeteringEnabled属性为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回调方法。

//注意设置AVAudioRecordermeteringEnabled属性为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的配置项,可以参考这个帖子


分享到:
评论

相关推荐

    ios-ios原生实现录音功能-AVFoundation-AVAudioRecorder.zip

    我在git上也上传了一份https://github.com/yuanjunxiao/ios-AVAudioRecorder-.git

    ios-AVAudioRecorderDemo.zip

    AVAudioRecorder 的使用演示

    iOS 10 App Development Essentials

    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 ...

    wav音频的录制--&gt;转成amr

    1 使用AVAudioRecorder录音成wav文件 2 为了避免录音启动耗时0 5秒(因为我录音时会有个mic图片的gif 发现显示gif显示被延时0 5秒) 所以 开启子线程做录音的operation 3 为了让可恶的android也能使用 需要将wav &gt...

    IOS 20个实用例子.zip

    -- IOS利用AVFoundation框架实现录音和播放 (AVAudioSession AVAudioRecorder AVAudioPlayer) -- IOS之分析网易新闻存储数据 CoreData的使用 增删改查 -- IOS二维码扫描Demo -- 18个 ios 项目源代码 -- iOS通讯录...

    IOS语音聊天实现

    添加AVFoundation Framework 为使用AVAudioRecorder类,我们需要向项目添加AVFoundation framework: 在项目Groups & Files面板上展开Targets 按Control-点击或右击MicBlow 选择Add &gt; Existing Frameworks… 按下...

    IOS利用AVFoundation框架实现录音和播放 AVAudioSession AVAudioRecorder AVAudioPlayer

    利用AVAudioRecorder录音 利用AVAudioPlayer播放 AVAudioSession录音权限 简单的录音播放demo,类似微信的按下录音,松开录音完成,并播放。

    iOS.8.App.Development.Essentials

    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 ...

    IOS利用AVFoundation框架实现录音和播放 (AVAudioSession AVAudioRecorder AVAudioPlayer)

    利用AVAudioRecorder录音 利用AVAudioPlayer播放 AVAudioSession录音权限 简单的录音播放demo,类似微信的按下录音,松开录音完成,并播放。

    iOS.9.App.Development.Essentials

    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 音频录制

    iOS AVFoundation框架下的AVAudioRecorder录音机的实现。

    YZSimpleAudioKit:AVAudioPlayer 和 AVAudioRecorder 的一些“包装类”或自定义类

    YZSimpleAudioKit 一些用于 AVAudioPlayer 和 AVAudioRecorder 的“包装类”或自定义类。

    iOS录音播放方法Demo

    二、使用AVAudioRecorder录音; 三、使用AVAudioPlayer播放,并添加播放动画; 四、使用lame将caf音频转化为mp3; 五、将mp3 转化为 base64编码; 六、查看录音文件大小; 七、删除语音文件;

    ios-音频的录制和播放(或是播放音乐).zip

    音频的录制与播放,进行封装后的单例工具组件。 (1)使用AVAudioRecorder进行录音 (2)使用AVAudioPlayer进行音频播放 SYAudio:https://github.com/potato512/SYAudio

    iOS多媒体音频(下)-录音及其播放的实例

    上一篇中总结了iOS中音效和音频播放的最基本使用方法,其中音频的播放控制是使用AVFoundation.framework框架中的AVAudioPlayer播放器对象来实现的,而这里音频的录制则是使用了同样框架下的一个叫AVAudioRecorder的...

    iOS开发项目- 基于WebSocket的聊天通讯(2)

    在AVFoundation框架中有一个AVAudioRecorder类专门处理录音操作,它同样支持多种音频格式。与AVAudioPlayer类似,你完全可以将它看成是一个录音机控制类,下面是常用的属性和方法: 先来了解下AVAudioRecorder的常用...

    IOS开发实现录音功能

    AVAudioRecorder *audioRecorder; } @end 在ViewDidLoad中: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); ; button.b

    iOS的音频文件的格式转换示例

    背景 因为我的公司需要设计到app与硬件的通信,所以去年深入...由于App是通过AVAudioRecorder录制音频,默认格式为pcm,文件比较大,所以不适合用于聊天通信的文件格式,所以最优的选择是转换成amr格式 音频格式转换方式

    iOS音频操作

    播放本地音频、AVAudioRecorder和Audio Queue录制音频、拍照等功能

    音频的录制和播放 iOS

    作者potato512,源码SYAudio,音频的录制与播放,进行封装后的单例工具组件。(1)使用AVAudioRecorder进行录音(2)使用AVAudioPlayer进行音频播放

Global site tag (gtag.js) - Google Analytics