Detecting blowing on the iPhone microphone?

前端 未结 5 2097
再見小時候
再見小時候 2020-12-23 19:01

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 ([[SCListen         


        
5条回答
  •  遥遥无期
    2020-12-23 19:39

    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;**
        }
    }
    

提交回复
热议问题