iPhone 麦克风电平:代码在一个项目中有效,在另一个项目中无效

发布于 2024-11-18 07:06:16 字数 1849 浏览 3 评论 0原文

教程 代码适用于我。我将其设置为吐出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文