首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动检测没有声音AVrecorder

自动检测没有声音AVrecorder
EN

Stack Overflow用户
提问于 2013-03-18 17:59:19
回答 1查看 178关注 0票数 0

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

有什么办法吗?我已经录好了:

代码语言:javascript
复制
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];

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2013-03-18 18:07:20

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

代码语言:javascript
复制
[anAVAudioRecorder setMeteringEnabled:YES];

然后你可以定期调用:

代码语言:javascript
复制
[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(即可能进入正值范围)。

[apple docs]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15473970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档