Guest User

Untitled

a guest
Jun 21st, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. private func setupAudioSession() {
  2. do {
  3.  
  4. try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
  5. try AVAudioSession.sharedInstance().setActive(true)
  6.  
  7. setupAudioNotifications()
  8.  
  9. } catch {
  10. print(error)
  11. }
  12. }
  13.  
  14. private func setupAudioNotifications() {
  15. NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: .AVAudioSessionInterruption, object: nil)
  16. }
  17.  
  18. @objc func handleInterruption(notification: Notification) {
  19.  
  20. guard let userInfo = notification.userInfo,
  21. let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
  22. let type = AVAudioSessionInterruptionType(rawValue: typeValue) else {
  23. return
  24. }
  25.  
  26. if type == .began { // при звонке .began срабатывает всегда
  27. // Interruption began, take appropriate actions
  28. Player.shared.stop()
  29.  
  30. } else if type == .ended { // а вот когда звонок заканчивается (любой из сторон), .ended не срабатывает
  31. if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
  32. let options = AVAudioSessionInterruptionOptions(rawValue: optionsValue)
  33. if options.contains(.shouldResume) {
  34. // Interruption Ended - playback should resume
  35. Player.shared.start()
  36. } else {
  37. // Interruption Ended - playback should NOT resume
  38. }
  39. }
  40. }
  41. }
Add Comment
Please, Sign In to add comment