【问题标题】:Handling interruptions during a phone call - the "begin" works but the "end" doesn't在通话过程中处理中断 - “开始”有效,但“结束”无效
【发布时间】:2016-04-17 14:38:27
【问题描述】:

应用程序录制音频,并在出现中断(例如电话呼叫)时停止录制音频,然后在电话结束时恢复录制音频。

应用程序当前在有电话时注册,但是当我挂断电话时,应用程序没有注册中断已经结束。 (我没有打开其他应用程序)。

使用函数委托

在我的 ViewController 中查看下面的代码。

func audioRecorderBeginInterruption(recorder: AVAudioRecorder){
    print("* * * * * * * inside begin interruption")}

func audioRecorderEndInterruption(recorder: AVAudioRecorder, withFlags flags: Int) {
    // THIS NEVER PRINTS, EVEN WHEN PHONE IS HUNG UP
    print("* * * * * * * inside end interruption")
}

有通知

我也尝试通过通知处理中断,但 .Ended 仍未处理,除非我接到电话并拒绝接听电话。查看我的 AppDelegate 中的代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let center = NSNotificationCenter.defaultCenter()

        center.addObserver(self,
            selector:"sessionInterrupted:",
            name:AVAudioSessionInterruptionNotification,
            object:AVAudioSession.sharedInstance()) //self.myViewController.audioSession)


        return true
    }



 func sessionInterrupted(notification: NSNotification){
                if let typeValue = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber{
                    if let type = AVAudioSessionInterruptionType(rawValue: typeValue.unsignedLongValue){
                        if type == .Began{
                            print("interruption: began")
                        } else{
                            // THIS ALSO NEVER PRINTS
                            print("interruption: ended")
                        }
        }

对我不起作用的相关解决方案

  1. 解决方案:添加无效的[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  2. 解决方案:使用MixWithOthers,它可以用于恢复播放音频,但在我的情况下不适用于恢复录制音频

当前假设

我目前的假设是,“结束”中断仅适用于诸如当您接到电话并且您拒绝接听电话时的中断,而不适用于您实际上拿起电话,聊了一会儿,然后挂断。我的猜测是,如果不使用越狱手机,就无法检测到后一种情况。

在这里以更广泛的方式展开:iOS AVAudioSession interruption notification not working as expected

这个假设似乎在DropVox's FAQ得到验证:

一次只有一个应用可以控制音频。如果 DropVox 正在录制 另一个应用程序控制,这就是所谓的“中断”。什么时候 中断结束,DropVox 只能恢复 记录如果它在前台7,这就是为什么我们告诫不要 使用“在后台录制”设置。

我可以通过使用routing 来检测是否正在使用麦克风来处理中断。但我不认为我可以在后台重新激活录音,所以一旦应用程序回到前台,我就会这样做。

是吗?

【问题讨论】:

  • 你找到解决办法了吗?
  • Apple 文档中确实有一行 (developer.apple.com/library/content/documentation/Audio/…) 说:“不能保证开始中断会导致结束中断。您的应用需要注意切换到前台运行状态或用户按下播放按钮。无论哪种情况,请确定您的应用是否应重新激活其音频会话。"

标签: ios swift avaudiorecorder avaudiosession interruptions


【解决方案1】:

不要applicationWillResignActive:/applicationDidBecomeActive: 函数中激活音频会话!

当电话到达时,系统拥有音频会话,此时如果您在applicationWillResignActive:/applicationDidBecomeActive:函数中调用函数[[AVAudioSession sharedInstance] setActive:YES error:&error],您的应用程序将拥有音频会话(实际上应该是在激活音频会话时失败)电话,但我不知道为什么它返回true)并且不会收到结束中断。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多