0

I want to detect the sound while recording that. If the sound stops for 2-3 seconds then the recording should stop automatically.

is there any way? I had done recording :-

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];

Thanks in advance

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
ios developer
  • 3,363
  • 3
  • 51
  • 111

1 Answers1

1

You shoudl use AVAudioRecorder support for audio level metering to keep track of the audio levels and stop recording when the levels are below a certain threshold. To enable metering -

[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];

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]

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264