此tutorial代码适用于我。我把它设置为吐出lowPAssResult的值。确实如此,在手机上运行它......
2011-07-04 14:28:08.672 MicBlow [280:707]检测到麦克风:0.036449 ......等等,如果我吹进它,价值就会上升。一切都好。
但是当我把它放在我自己的项目框架中时,它不起作用。我把这段代码放在我的init函数中......
//Set up the mic stuff
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 record];
recorder.meteringEnabled = YES;
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
}
并添加了此回调
- (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;
NSLog(@"Mic blow detected - peakPowerForChannel : %f, lowPassResults: %f",peakPowerForChannel,lowPassResults);
}
输出看起来像这样;
2011-07-04 14:30:32.974 MinuteGames [290:707]检测到麦克风:0.000001 反复。每一帧
无论我吹多少。请帮忙,我相信这一定很简单!