Note that there are some explanatory texts on larger screens.

plurals
  1. PODetecting blow in the iPhone Mic with Cocos2D
    primarykey
    data
    text
    <p>I'm using Cocos2D and the system particles and I hope I wrote this correctly in English. I'm having problems to recognize sounds with the iPhone mic in certain way. </p> <p>I have different sections in my application, in one of them I use the mic to detect if someone is "blowing air" into the mic. This part works fine at the beginning, but if you go to other section of the app that plays sound and later you return to this area and try to blow air, it won't work.</p> <p>I debuged the code, and the levelTimeCallback is always working even if I'm not in this scene. I don't really know what's happening. I've stopped all sounds using </p> <p><code>[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];</code></p> <p>Anyone knows what I'm doing wrong? BTW works perfectly in simulator, but not in iPhone.</p> <p>The recorder is set in the onEnter method</p> <pre><code>-(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:&amp;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"); </code></pre> <p>}</p> <p>This is the levelTimerCallback method were the sound is "checked" </p> <pre><code>- (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 &gt; 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");} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload