【问题标题】:AVAudioSessionInterruptionNotification with AVAudioSessionInterruptionWasSuspendedKey triggered when trying to resume from background尝试从后台恢复时触发带有 AVAudioSessionInterruptionWasSuspendedKey 的 AVAudioSessionInterruptionNotification
【发布时间】:2019-07-25 14:13:33
【问题描述】:

AVAudioPlayer 在后台播放音频,用户通过耳机控件或锁定屏幕控件将其暂停。 如果用户在 30 秒内执行此操作并尝试恢复 - 一切正常。 如果用户尝试在 30 秒内恢复它 - 音频开始播放,但一秒后来自 AVAudioSessionDelegateaudioSessionInterruptionNotification 被触发,AVAudioSessionInterruptionWasSuspendedKey YES。

之后,应用程序完全停止对远程控制事件做出反应。正在触发事件,但 AVAudioPlayer 不会对任何命令做出反应。事实上,[[self audioplayer] isAudioPlaying] 在播放继续时返回 NO。

如果我尝试按以下方式处理它 - 它会有所帮助(所以我将 AVAudioSession 设置为不活动,然后 playHelper 方法将其激活并播放音频),但是有一个小故障,因为通知在它开始​​播放后被触发.

- (void)audioSessionInterruptionNotification:(NSNotification *)interruption {
    UInt8 interruptionType = [[interruption.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] intValue];

    NSLog(@"Session interrupted > --- %s ---\n", interruptionType == AVAudioSessionInterruptionTypeBegan ? "Begin Interruption" : "End Interruption");
    NSDictionary *notificationUserInfo = [interruption userInfo];
    if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
        if ([notificationUserInfo valueForKey:AVAudioSessionInterruptionWasSuspendedKey]) {
            [[AVAudioSession sharedInstance] setActive:false error:nil];
            [self playHelper];
        } else {
            [self interruptionStarted];
        }
    } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
        [self interruptionEnded:(NSUInteger)[notificationUserInfo objectForKey:AVAudioSessionInterruptionOptionKey]];
    }
}

请告知可能是什么原因造成的。

【问题讨论】:

    标签: ios objective-c avaudioplayer avaudiosession


    【解决方案1】:

    AVAudioSessionInterruptionWasSuspendedKey 通知被抛出,因为当您的应用程序进入后台并且没有播放任何内容时,您应该停用音频会话。如果您的播放器在用户从锁定屏幕暂停时正在播放,您不必停用会话。

    当你的应用程序被发送到后台时,你应该监听通知,当它被激活时,Swift 中的代码应该是这样的。

    NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: .main) { sender in
                guard self.player.isPlaying == false else { return }
                self.setSession(active: false)
            }
    
    NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { sender in
                guard self.player.isPlaying else { return }
                self.setSession(active: true)
            }
    
        func setSession(active: Bool) -> Bool {
            let session = AVAudioSession.sharedInstance()
            do {
                try session.setCategory(.playback, mode: .default)
                try session.setActive(active)
                return true
            } catch let error {
                print("*** Failed to activate audio session: \(error.localizedDescription)")
                return false
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多