Reputation: 3095
I’m using AVAudioSession
and AVAudioRecorder
to record audio in my app. Unfortunately, my app freezes when I get a call while recording. I tried implementing the AVAudioRecorder
delegate methods for interruptions, but they aren’t being called. I have a view controller that the user interacts with to initiate and stop the recording process and a separate recorder controller that contains the AVAudioRecorder
.
I have the following implemented in my recorder controller:
@interface OSRecordPlayController : NSObject <AVAudioRecorderDelegate, AVAudioPlayerDelegate, AVAudioSessionDelegate>
-(void) audioRecorderBeginInterruption:(AVAudioRecorder *)recorder
{
if(recorder.recording)
{
[recorder stop];
[self stopRecording];
[self moveViewsDown];
interruptedDuringRecording = YES;
}
}
-(void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withOptions:(NSUInteger)flags
{
if(interruptedDuringRecording)
{
[recorder prepareToRecord];
[recorder record];
interruptedDuringRecording = NO;
}
}
-(void)setUpAudioFile
{
self.recorder = [[AVAudioRecorder alloc] initWithURL:self.audioFileURL settings:audioSettings error:nil];
self.recorder.delegate = self;
self.recorder.meteringEnabled = YES;
[self.recorder prepareToRecord];
}
Why won’t these get called? I set a break point for both, but I hit neither of them. I’ve seen this question quite a bit with no solid answers, all the old answers are for AVAudioSession
delegate methods for interruption, but those have been deprecated.
Upvotes: 0
Views: 972
Reputation: 197
One thing that can cause the delegate methods not to be called is if the AVAudioRecorder object gets garbage collected before the recording is finished. This would happen if no other object retains a reference to the recorder.
Of course, I didn't stupidly actually make that error that myself...really...
Upvotes: 1
Reputation: 3838
Use notifications instead of the delegate. Here it is in Swift.
NSNotificationCenter.defaultCenter().addObserver(self,
selector:"sessionInterrupted:",
name:AVAudioSessionInterruptionNotification,
object:myRecorder)
Upvotes: 3