ios - 自动检测AVrecorder没有声音

标签 ios objective-c avaudiorecorder avaudiosessiondelegate

我想在录制时检测声音。如果声音停止 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];

提前致谢

最佳答案

您应该使用 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];

threshold: A floating-point representation, in decibels, of a given audio channel’s current average power. A return value of 0 dB indicates full scale, or maximum power; a return value of -160 dB indicates minimum power (that is, near silence).

If the signal provided to the audio player exceeds ±full scale, then the return value may exceed 0 (that is, it may enter the positive range).

[apple docs]

关于ios - 自动检测AVrecorder没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15473970/

相关文章:

ios - 用户必须登录才能使用 native 共享框

ios - 在 IOS 中使用 AVAudioRecorder 录制声音时音量正在下降

ios - 如何在 ViewController 中获取委托(delegate)方法以查看 *recorder 何时完成(不同的类)

ios - 使用未解析的标识符 ParseCrashReporting

ios - Swift 像闭包一样使用选择器参数

ios - 是否可以将同一个 ViewController.view 作为单例添加到不同的 View 中作为 subview ?

iphone - 如何高效搜索iOS通讯录

ios - 如何发送固定时间的推送通知?

iphone - iPad 中的异常,UIImagePickerController 必须通过 UIPopoverController 呈现

iOS 7 m4a 到 mp3 编码 - 这可能吗?