iphone - 如何在 iphone 中检测麦克风的打击而不是语音?

标签 iphone objective-c ipad

我正在使用此代码来检测打击。

可是我受不了了。我有声音。

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

但是没有声音。

最佳答案

您必须尝试使用​​低通结果。试试这个:

- (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;  
    //NSLog(@"%f", lowPassResults);
    if (lowPassResults > 0.55)
        NSLog(@"Mic blow detected");
}

关于iphone - 如何在 iphone 中检测麦克风的打击而不是语音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286768/

相关文章:

iphone - 如何从其他 .h 文件导入 IBActions?

ios4 - SQLITE3插入空值

iphone - NSPredicate 在 NSFetchedResultsController 中表现得很奇怪

objective-c - 是调用performSelector的对象:withObject:afterDelay get retained by the NSRunLoop?

ios - 遍历 UITableViewCells 的奇怪行为

python - objective C/iOS 中 python 的 file.read() 的等价物是什么?

ios - 方向改变时居中多个标签?

iphone - 更改 iOS 中的地理位置权限通知文本

iphone - 在移动设备(iphone、android 等)上查看 trac

iphone - 有没有办法改变 scrollRectToVisible 的速度?