AVAudioRecorder不更新peakPowerForChannel iphone

时间:2012-02-06 12:38:31

标签: iphone objective-c avaudiorecorder

对于我的cocos 2d游戏,我想从iphone的麦克风中检测语音,它应该自动播放,我正在使用avaudiorecorder的peakPowerForChannel 代码:

const double ALPHA = 0.05;
    NSString *currentRecorder=@"nil";
            NSError *error;
    if(checker.recording){
        [checker updateMeters];
        currentRecorder=@"checker";
        peakPowerForChannel = pow(10, (ALPHA* [checker peakPowerForChannel:0]));
    }
    else if (recorder.recording) {
        [recorder updateMeters];
        peakPowerForChannel = pow(10, (ALPHA * [recorder peakPowerForChannel:0]));
         currentRecorder=@"recorder";
    }
   // NSLog(@"-------------- %f",lowPassResults);
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;  
    NSLog(@"%@ >>>>>>>>>>>>>>>>>>>>>> %f   peakPowerForChannel ---%f",currentRecorder,lowPassResults,peakPowerForChannel);
  if (lowPassResults >0.4){
        if (!recorder.recording) {
            [checker stop];
}
    else{
        if (recorder.recording) {
            [recorder stop];
 [checker record];

        }
    }

第一次工作正常。播放音频后,[recorder peakPowerForChannel:0]给出一个恒定值 我该如何解决这个问题

谢谢

1 个答案:

答案 0 :(得分:5)

[recorder updateMeters]仅更新一次值。每次检查[recorder peakPowerForChannel:0]值时。您必须调用[recorder updateMeters]方法。 好的方法是检查计时器中的值并在检查值之前调用[recorder updateMeters]方法。