iPhone 麦克风电平:代码在一个项目中有效,在另一个项目中无效
此 教程 代码适用于我。我将其设置为吐出 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 反复。每一帧
无论我吹了多少。请帮忙,我确信这一定很简单!
This tutorial code works fine for me. I set it up to spit out the value of lowPAssResult. Run it on the phone, sure enough, as expected...
2011-07-04 14:28:08.672 MicBlow[280:707] Mic blow detected : 0.036449
...etc, The value goes up if I blow into it. All good.
But when I take it and put it in my own project framework, it doesn't work. I put this code in my init function...
//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];
}
And added this callback
- (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);
}
And the output looks like this;
2011-07-04 14:30:32.974 MinuteGames[290:707] Mic blow detected : 0.000001
repeatedly. Every frame
No matter how much blowing I do. Please help, I'm sure this must be somethgn simple!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论