Created
June 17, 2019 14:54
-
-
Save AmitaiB/c9d04b4ab5444637449de7b15bda602a to your computer and use it in GitHub Desktop.
Elegant implementation of iOS Audio Interruption Notification subscription Monitor in Swift, from Quran.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AudioInterruptionMonitor.swift | |
// QueuePlayer | |
// | |
// Created by Afifi, Mohamed on 4/29/19. | |
// Copyright © 2019 Quran.com. All rights reserved. | |
// | |
// originally DL'ed from https://github.com/quran/quran-ios/blob/master/QueuePlayer/AudioInterruptionMonitor.swift | |
import AVFoundation | |
enum AudioInterruptionType { | |
case began | |
case endedShouldResume | |
case endedShouldNotResume | |
} | |
protocol AudioInterruptionMonitorDelegate: class { | |
func onAudioInterruption(type: AudioInterruptionType) | |
} | |
class AudioInterruptionMonitor { | |
weak var delegate: AudioInterruptionMonitorDelegate? | |
init() { | |
let center = NotificationCenter.default | |
center.addObserver(self, | |
selector: #selector(onInterruption(_:)), | |
name: AVAudioSession.interruptionNotification, | |
object: nil) | |
} | |
deinit { | |
NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: nil) | |
} | |
@objc private func onInterruption(_ notification: Notification) { | |
guard let info = notification.userInfo else { | |
return | |
} | |
guard let rawType = info[AVAudioSessionInterruptionTypeKey] as? UInt else { | |
return | |
} | |
guard let type = AVAudioSession.InterruptionType(rawValue: rawType) else { | |
return | |
} | |
switch type { | |
case .began: delegate?.onAudioInterruption(type: .began) | |
case .ended: | |
guard let rawOptions = info[AVAudioSessionInterruptionOptionKey] as? UInt else { | |
return | |
} | |
let options = AVAudioSession.InterruptionOptions(rawValue: rawOptions) | |
if options.contains(.shouldResume) { | |
delegate?.onAudioInterruption(type: .endedShouldResume) | |
} else { | |
delegate?.onAudioInterruption(type: .endedShouldNotResume) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment