AVplayer resuming after incoming call

前端 未结 6 1552
心在旅途
心在旅途 2020-12-04 15:48

I am using AVPlayer for music playback. My problem is that after an incoming call, the player won\'t resume. How do I handle this when an incoming call comes?

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 16:39

    I have to wait for 2 seconds to make it work.

            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    
                self.player.play()
            }
    

    The whole func:

    func playerInterruption(notification: NSNotification) {
    
        guard let userInfo = notification.userInfo,
            let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
            let type = AVAudioSessionInterruptionType(rawValue: typeValue) else {
                return
        }
        if type == .began {
            // Interruption began, take appropriate actions (save state, update user interface)
            player.pause()
        }
        else if type == .ended {
    
            guard let optionsValue =
                userInfo[AVAudioSessionInterruptionOptionKey] as? UInt else {
    
                    return
            }
            let options = AVAudioSessionInterruptionOptions(rawValue: optionsValue)
            if options.contains(.shouldResume) {
                // Interruption Ended - playback should resume
    
                DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    
                    self.player.play()
                }
    
            }
        }
    }
    

提交回复
热议问题