IOS 通过麦克风检测声音分贝

来源:互联网 发布:金窗教务系统源码 编辑:程序博客网 时间:2024/04/29 01:59

在iphone真机上测的数据始始终没有变化, 在模拟器上是可以的,折腾了半天,终于搜索找到解决方法,在代码中添加

[objc] view plaincopy
  1. [[AVAudioSession sharedInstance]  
  2.         setCategory: AVAudioSessionCategoryPlayAndRecord error: nil nil];  



1. 开发环境xocde5.1.1 , IOS sdk7.1

2.  引入框架:AVFoundation.framework

3.  部分源码如下:

[objc] view plaincopy
  1. #import "MicBlowViewController.h"  
  2.   
  3. @implementation MicBlowViewController  
  4.   
  5. - (void)viewDidLoad  
  6. {  
  7.     [super viewDidLoad];  
  8.       
  9.      /* 必须添加这句话,否则在模拟器可以,在真机上获取始终是0  */  
  10.     [[AVAudioSession sharedInstance]  
  11.         setCategory: AVAudioSessionCategoryPlayAndRecord error: nil nil];  
  12.   
  13.     /* 不需要保存录音文件 */  
  14.     NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];  
  15.       
  16.     NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:  
  17.                 [NSNumber numberWithFloat44100.0], AVSampleRateKey,  
  18.                 [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,  
  19.                 [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,  
  20.                 [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,  
  21.                 nil nil];  
  22.           
  23.     NSError *error;  
  24.     recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];  
  25.     if (recorder)  
  26.     {  
  27.         [recorder prepareToRecord];  
  28.         recorder.meteringEnabled = YES;  
  29.         [recorder record];  
  30.         levelTimer = [NSTimer scheduledTimerWithTimeInterval1 targetself selector@selector(levelTimerCallback:) userInfo: nil repeats: YES];  
  31.     }  
  32.     else  
  33.     {  
  34.         NSLog(@"%@", [error description]);  
  35.     }  
  36.       
  37. }  
  38.   
  39.   
  40. /* 该方法确实会随环境音量变化而变化,但具体分贝值是否准确暂时没有研究 */  
  41. - (void)levelTimerCallback:(NSTimer *)timer {  
  42.     [recorder updateMeters];  
  43.    
  44.     float   level;                // The linear 0.0 .. 1.0 value we need.  
  45.     float   minDecibels = -80.0f// Or use -60dB, which I measured in a silent room.  
  46.     float   decibels    = [recorder averagePowerForChannel:0];  
  47.       
  48.     if (decibels < minDecibels)  
  49.     {  
  50.         level = 0.0f;  
  51.     }  
  52.     else if (decibels >= 0.0f)  
  53.     {  
  54.         level = 1.0f;  
  55.     }  
  56.     else  
  57.     {  
  58.         float   root            = 2.0f;  
  59.         float   minAmp          = powf(10.0f0.05f * minDecibels);  
  60.         float   inverseAmpRange = 1.0f / (1.0f - minAmp);  
  61.         float   amp             = powf(10.0f0.05f * decibels);  
  62.         float   adjAmp          = (amp - minAmp) * inverseAmpRange;  
  63.           
  64.         level = powf(adjAmp, 1.0f / root);  
  65.     }  
  66.       
  67.     /* level 范围[0 ~ 1], 转为[0 ~120] 之间 */  
  68.     dispatch_async(dispatch_get_main_queue(), ^{  
  69.         [_textLabel setText:[NSString stringWithFormat:@"%f", level*120]];  
  70.     });  
  71. }  
  72.   
  73.   
  74. - (void)dealloc {  
  75.     [levelTimer release];  
  76.     [recorder release];  
  77.     [_textLabel release];  
  78.     [_cLabel release];  
  79.     [super dealloc];  
  80. }  
  81.   
  82. @end  
0 0