0

我想在录制时检测声音。如果声音停止 2-3 秒,则录音应自动停止。

有什么办法吗?我已经完成了录制:-

NSArray *dirPaths;
        NSString *docsDir;

        dirPaths = NSSearchPathForDirectoriesInDomains(
                                                       NSDocumentDirectory, NSUserDomainMask, YES);
        docsDir = [dirPaths objectAtIndex:0];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"ddMMyyyyhh:mm:ss"];

        NSDate *now = [[NSDate alloc] init];
        NSString *dateString = [dateFormatter stringFromDate:now];
        dateString=[NSString stringWithFormat:@"%@.caf",dateString];
        soundFilePath = [docsDir
                                   stringByAppendingPathComponent:dateString];
        NSLog(@"soundFilePath==>%@",soundFilePath);
        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
        [soundFilePath retain];
        NSDictionary *recordSettings = [NSDictionary
                                        dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithInt:AVAudioQualityMin],
                                        AVEncoderAudioQualityKey,
                                        [NSNumber numberWithInt:16],
                                        AVEncoderBitRateKey,
                                        [NSNumber numberWithInt: 2],
                                        AVNumberOfChannelsKey,
                                        [NSNumber numberWithFloat:44100.0],
                                        AVSampleRateKey,
                                        nil];
        NSError *error = nil;
        recorder = [[AVAudioRecorder alloc]
                    initWithURL:soundFileURL
                    settings:recordSettings
                    error:&error];
        if (error)
        {
            NSLog(@"error: %@", [error localizedDescription]);
        } else {
            [recorder prepareToRecord];
        }
        [recorder record];

提前致谢

4

1 回答 1

1

您应该使用AVAudioRecorder对音频电平计量的支持来跟踪音频电平并在电平低于某个阈值时停止录制。要启用计量 -

[anAVAudioRecorder setMeteringEnabled:YES];

然后你可以定期调用:

[anAVAudioRecorder updateMeters];
power = [anAVAudioRecorder averagePowerForChannel:0];
if (power > threshold && anAVAudioRecorder.recording==NO)
    [anAVAudioRecorder record];
else if (power < threshold && anAVAudioRecorder.recording==YES)
    [anAVAudioRecorder stop];

阈值:给定音频通道当前平均功率的浮点表示,以分贝为单位。返回值 0 dB 表示满量程或最大功率;返回值 -160 dB 表示最小功率(即接近静音)。

如果提供给音频播放器的信号超过±满量程,则返回值可能超过0(即可能进入正范围)。

[苹果文档]

于 2013-03-18T10:07:20.480 回答