IOS AVAudioRecorder, How to record only when audio input present (non-silence)

Martha picture Martha · Oct 4, 2010 · Viewed 13.4k times · Source

I'm using AVAudioRecorder to record audio from the iphone's mic but I want to discard the silence periods: start recording when detecting sound, and stop recording when next silence.

Can't figure out how to do that

Any advice?

Thanx!

Answer

Jake picture Jake · Oct 4, 2010

Perhaps you could use the AVAudioRecorder's support for audio level metering to keep track of the audio levels and enable recording when the levels are above a given threshold. You'd need to enable metering with:

[anAVAudioRecorder setMeteringEnabled:YES];

and then you could periodically call:

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

Or something like that.