Alex Rivera | Logout

Detecting blowing on the iPhone microphone?

Asked 2009-04-28T02:06:00.630
19

I am trying to detect when the user is blowing into the mic of an iPhone. Right now I am using the SCListener class from Stephen Celis to call

if ([[SCListener sharedListener] peakPower] > 0.99)

in an NSTimer. However, this returns true sometimes when I'm not blowing. Anyone have any sample code to check if the user is blowing into the mic?

Edit
Report

2 Answers

11

Use return as lowPassResults is bigger than 0.55. This is working fine:

-(void)readyToBlow1 { 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.01 target: self selector: @selector(levelTimerCallback1:) userInfo: nil repeats: YES];
    } else
        NSLog(@"%@",[error description]);
}

-(void)levelTimerCallback1:(NSTimer *)timer { [recorder updateMeters];
    const double ALPHA = 0.05; 
    double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0])); 
    double lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults; 
    if (lowPassResults > 0.55) { 
        lowPassResults = 0.0;
        [self invalidateTimers];
        NextPhase *objNextView =[[NextPhase alloc]init];
        [UIView transitionFromView:self.view
                      toView:objNextView.view
                      duration:2.0
                      options:UIViewAnimationOptionTransitionCurlUp
                      completion:^(BOOL finished) {}
        ];
        [self.navigationController pushViewController:objNextView animated:NO];
    **return;**
    }
}
answered 2013-01-23T14:14:49.447
4

http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/

this tutorial works fine with simulator but its not working in iphone there is no response from iphone mic

answered 2012-12-06T12:09:20.483

Your Answer