IOS 通过麦克风检测声音分贝
来源:互联网 发布:金窗教务系统源码 编辑:程序博客网 时间:2024/04/29 01:59
在iphone真机上测的数据始始终没有变化, 在模拟器上是可以的,折腾了半天,终于搜索找到解决方法,在代码中添加
- [[AVAudioSession sharedInstance]
- setCategory: AVAudioSessionCategoryPlayAndRecord error: nil nil];
1. 开发环境xocde5.1.1 , IOS sdk7.1
2. 引入框架:AVFoundation.framework
3. 部分源码如下:
- #import "MicBlowViewController.h"
- @implementation MicBlowViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- /* 必须添加这句话,否则在模拟器可以,在真机上获取始终是0 */
- [[AVAudioSession sharedInstance]
- setCategory: AVAudioSessionCategoryPlayAndRecord error: nil nil];
- /* 不需要保存录音文件 */
- NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
- NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
- [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
- [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
- [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
- nil nil];
- NSError *error;
- recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
- if (recorder)
- {
- [recorder prepareToRecord];
- recorder.meteringEnabled = YES;
- [recorder record];
- levelTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
- }
- else
- {
- NSLog(@"%@", [error description]);
- }
- }
- /* 该方法确实会随环境音量变化而变化,但具体分贝值是否准确暂时没有研究 */
- - (void)levelTimerCallback:(NSTimer *)timer {
- [recorder updateMeters];
- 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 = [recorder averagePowerForChannel:0];
- if (decibels < minDecibels)
- {
- level = 0.0f;
- }
- else if (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);
- }
- /* level 范围[0 ~ 1], 转为[0 ~120] 之间 */
- dispatch_async(dispatch_get_main_queue(), ^{
- [_textLabel setText:[NSString stringWithFormat:@"%f", level*120]];
- });
- }
- - (void)dealloc {
- [levelTimer release];
- [recorder release];
- [_textLabel release];
- [_cLabel release];
- [super dealloc];
- }
- @end
0 0
- IOS 通过麦克风检测声音分贝
- IOS 通过麦克风检测声音分贝
- ios通过PCM计算声音分贝大小
- iOS 设备 检测声音输出设备及耳机麦克风的处理
- 声音分贝
- 资质android声音检测仪---分贝仪 (附有源码)
- ios 检测声音支持
- 通过网络发送和播放麦克风的声音
- iOS麦克风权限的检测和获取
- iOS 屏幕录制 (可选录入麦克风的声音)
- Android 获取麦克风的音量(分贝)
- Android 录音声音分贝获取
- 获取麦克风声音数据
- 相机检测 麦克风检测
- iOS 相机 麦克风 照片 权限的检测与获取
- 使用C#实现通过网络发送和播放麦克风的声音
- 麦克风没声音怎么办,解决麦克风没声音的方法
- 麦克风没有声音的设置
- “com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题解决
- 2015年12月8日日志
- Scala 文件读写及Scala控制台输入
- centos 6.5 修改yum源
- Intent和IntentFilter的区别
- IOS 通过麦克风检测声音分贝
- ios学习笔记(七)关于NSUInteger
- ScrollView嵌套ViewPager
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- imx6q处理器,linux操作系统平台搭建 从eMMC启动系统
- C#读取摄像头处理图片AForge
- POJ 3273 Monthly Expense(二分搜索,最大化最小值)
- libcur进行文件上传
- 创建、删除、索引和更改表