O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.4. Handling Interruptions While Recording Audio

Problem

You want your AVAudioRecorder instance to be able to resume recording after an interruption, such as an incoming phone call.

Solution

Implement the audioRecorderBeginInterruption: and audioRecorderEndInterruption:withOptions: methods of the AVAudioRecorderDelegate protocol in the delegate object of your audio recorder, and resume the recording process by invoking the record instance method of your AVAudioRecorder when the interruption has ended:

- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{

    NSLog(@"Recording process is interrupted");

}

- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder
                         withOptions:(NSUInteger)flags{

    if (flags == AVAudioSessionInterruptionOptionShouldResume){
        NSLog(@"Resuming the recording...");
        [recorder record];
    }

}

Discussion

Just like audio players (instances of AVAudioPlayer), audio recorders of type AVAudioRecorder also receive delegate messages whenever the audio session associated with them is deactivated because of an interruption. The two methods mentioned in this recipe’s Solution are the best places to handle such interruptions. In the case of an interruption to the audio recorder, you can invoke the record instance method of AVAudioRecorder after the interruption to continue the recording process. However, the recording will overwrite the previous recording, and all data recorded before the interruption will be lost.

Note

It is very important to bear in mind that when ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required