AVplayer resuming after incoming call

前端 未结 6 1521
心在旅途
心在旅途 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:17

    I was getting the same problem in "AVAudioPlayer" controller. Using "AVAudioSession.interruptionNotification" we can resume audio playing once interruption ended even though audio playing in the background.

        @objc func interruptionNotification(notification: Notification) {
        guard let type = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt,
            let interruption = AVAudioSession.InterruptionType(rawValue: type) else {
                return
        }
    
        if interruption == .began{
            print("Pause audio....")
            self.player?.pause()
            do {
                try AVAudioSession.sharedInstance().setActive(false)
                print("AVAudioSession is inactive")
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
    
        if interruption == .ended{
            print("Play audio....")
            do {
                try AVAudioSession.sharedInstance().setActive(true)
                print("AVAudioSession is Active again")
                self.player?.play()
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
    }
    

    Here first you need to InActive AVAudioSession when it's interrupted and Active AVAudioSession once interruption ended. It's working perfectly please try it!

提交回复
热议问题