9.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:withFlags: 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
withFlags:(NSUInteger)flags{
if (flags == AVAudioSessionInterruptionFlags_ShouldResume){
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 the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access