10.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
AVAudio
Recorder
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 ...
Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.