Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private func setupAudioSession() {
- do {
- try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
- try AVAudioSession.sharedInstance().setActive(true)
- setupAudioNotifications()
- } catch {
- print(error)
- }
- }
- private func setupAudioNotifications() {
- NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: .AVAudioSessionInterruption, object: nil)
- }
- @objc func handleInterruption(notification: Notification) {
- guard let userInfo = notification.userInfo,
- let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
- let type = AVAudioSessionInterruptionType(rawValue: typeValue) else {
- return
- }
- if type == .began { // при звонке .began срабатывает всегда
- // Interruption began, take appropriate actions
- Player.shared.stop()
- } else if type == .ended { // а вот когда звонок заканчивается (любой из сторон), .ended не срабатывает
- if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
- let options = AVAudioSessionInterruptionOptions(rawValue: optionsValue)
- if options.contains(.shouldResume) {
- // Interruption Ended - playback should resume
- Player.shared.start()
- } else {
- // Interruption Ended - playback should NOT resume
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment