18

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!

0

3 Answers 3

13

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.

3
  • 2
    Jake, thank you for the answer. Can't use metering as what it measures is average and peaks. If there is a mixed string of silences and audio input, it wont notice when theres a new silence after a silence-audio.
    – Martha
    Oct 5, 2010 at 10:51
  • 3
    Also, it looks like the meters only update when the AVAudioRecorder is recording.
    – arlomedia
    Jul 21, 2015 at 21:49
  • What's the threshold value? Jul 27, 2017 at 4:58
4

I've found the way based on Audio Queue Services. It is alot more complicated but alot more fun too as you define your queue buffers for the incoming audio packets.

You need to define the callback when the buffer if full, so you have the buffer full of packets that you can process as you wish, in my case to detect silence and a few more things.

Later having more time ill post the solution. If anyone urged that just cant wait drop me an email and ill be glad to help.

Check speakhere example here: http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html

2
  • 3
    "If anyone urged that just cant wait drop me an email and ill be glad to help" So where is Your Email id? Apr 14, 2012 at 13:37
  • @Martha. Can I use SpeakHear example in my project? Jun 3, 2015 at 12:57
1

You can determine the background audio level by recording to a garbage file before you start recording for real:

AVAudioRecorder stops working when writing to /dev/null on iPhone 5s with iOS 7

Not the answer you're looking for? Browse other questions tagged or ask your own question.