ios - AVAudioRecorder 中断委托(delegate)方法未被调用

标签 ios avfoundation avaudiorecorder

我正在使用 AVAudioSessionAVAudioRecorder 在我的应用程序中录制音频。不幸的是,当我在录音时接到电话时,我的应用程序会卡住。我尝试为中断实现 AVAudioRecorder 委托(delegate)方法,但它们没有被调用。我有一个 View Controller ,用户可以与之交互以启动和停止录音过程,还有一个单独的录音机 Controller ,其中包含 AVAudioRecorder

我在记录器 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];
}

为什么这些不会被调用?我为两者都设置了断点,但我都没有击中它们。我已经看过很多这个问题,但没有可靠的答案,所有旧答案都是针对 AVAudioSession 中断委托(delegate)方法,但这些已被弃用。

最佳答案

使用通知代替委托(delegate)。 在 Swift 中。

NSNotificationCenter.defaultCenter().addObserver(self,
        selector:"sessionInterrupted:",
        name:AVAudioSessionInterruptionNotification,
        object:myRecorder)

关于ios - AVAudioRecorder 中断委托(delegate)方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631478/

相关文章:

ios - 如何在具有两个不同单元格的 TableView 中添加一行

iphone - 无法同时播放 MPMoviePlayerController 和通过 AVAudioRecorder 进行录制?

iphone - 在 iOS、AVAudioRecorder 或其他设备上收听麦克风输入?

objective-c - 视频剪辑导出错误: 'Invalid output file type' in objective-c

ios - 在 AVPlayer 中切换视频会在更改时创建 Flash

ios - AVAudioEngine中AuAudioBuffer的回调实现

xcode - 录音语义问题

ios - 直接跳转到通常通过 TabBarViewController 中的选项卡之一访问的 View Controller

ios - UIKeyboardTypeURL 显示 '.co.uk' 而不是 '.com'

ios - 如何在 UIBezierPath 弧中获取半径的中心点?