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 に答える