我正在开发一个应用程序,允许孩子完成呼吸练习作为他们物理治疗过程的一部分,此刻我仍然只是想确认麦克风正在接收吹动的声音。我在NSLog中收到以下消息
2017-04-22 17:01:52.292319+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.322377+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.352402+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.382331+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.412318+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.442277+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.472358+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.502275+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.532323+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.562265+0100 microphone[2251:596932] Mic blow detected
2017-04-22 17:01:52.592255+0100 microphone[2251:596932] Mic blow detected
这是我目前的代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless],
AVFormatIDKey,
[NSNumber numberWithInt: 1],
AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax],
AVEncoderAudioQualityKey, nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(levelTimerCallBack:) userInfo:nil repeats:YES];
} else
NSLog(@"%@", [error description]);
}
- (void)levelTimerCallBack:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults < 0.000005)
NSLog(@"Mic blow detected");
}
@end
我最初有“if(lowPassResults&lt; 0.95)”,但根本没有检测到麦克风。我现在遇到的问题是它没有任何人吹入麦克风时检测到麦克风。我认为值(0.00005)仍然太高但找不到合适的值。任何人都可以建议我需要在这里进行调整吗?
提前致谢