【问题标题】:Detecting blow in the iPhone Mic with Cocos2D使用 Cocos2D 检测 iPhone Mic 中的打击
【发布时间】:2011-07-04 13:13:11
【问题描述】:

我正在使用 Cocos2D 和系统粒子,我希望我用英文正确地写了这个。 我在以某种方式识别 iPhone 麦克风的声音时遇到问题。

我的应用程序中有不同的部分,在其中一个部分中,我使用麦克风来检测是否有人向麦克风“吹气”。这部分一开始可以正常工作,但是如果您转到应用程序的其他播放声音的部分,然后您返回该区域并尝试吹气,它将无法正常工作。

我调试了代码,即使我不在这个场景中,levelTimeCallback 也一直在工作。我真的不知道发生了什么。我已经使用

停止了所有声音

[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];

谁知道我做错了什么?顺便说一句,在模拟器中完美运行,但在 iPhone 中却不行。

记录器在onEnter方法中设置

-(void) onEnter {
     [super onEnter];
     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];
          NSLog(@"I'm in the recorder");

       } else

              NSLog(@"recorder error");

}

这是 levelTimerCallback 方法,如果声音被“检查”了

- (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.55)
{   
    NSLog(@"Mic blow detected");

    self.emitter = [[CCParticleExplosion alloc] initWithTotalParticles:5];
    [self addChild: emitter z:1];           
    emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"hoja.png"];      
    emitter.autoRemoveOnFinish = YES;

}
NSLog(@"Inside levelTimerCallback");}

【问题讨论】:

    标签: audio cocos2d-iphone recorder


    【解决方案1】:

    同样的问题。 有两种方法可以解决这个问题。

    1:使用AVAudioPlayer播放音乐或音效。

    2:将#import "CDAudioManager.h"添加到AppDelegate.m并添加

     [CDAudioManager initAsynchronously:kAMM_PlayAndRecord];
    

    - (void) applicationDidFinishLaunching:(UIApplication*)application`
    

    source

    【讨论】:

    • 嗨@huyleit!谢谢回答!我找到的解决方案是:
    • 为了在 init 方法中添加一些代码,我用解决方案修改了我的原始帖子。非常感谢您的回答,我将不得不尝试您的解决方案。
    【解决方案2】:

    最后,在阅读了音频会话食谱和不同类型的相关帖子后,我找到了解决方案。只需将此代码块放在 init 方法中即可:

        UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
        AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
        AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
        AudioSessionSetActive(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多