Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchernan/de6c30b40fb69d03fb63 to your computer and use it in GitHub Desktop.
Save jchernan/de6c30b40fb69d03fb63 to your computer and use it in GitHub Desktop.
Sample usage of AVAudioSessionInterruptionNotification
func addInterruptionNotification() {
// add audio session interruption notification
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "handleInterruption:",
name: AVAudioSessionInterruptionNotification,
object: nil)
}
func handleInterruption(notification: NSNotification) {
if notification.name != AVAudioSessionInterruptionNotification
|| notification.userInfo == nil{
return
}
var info = notification.userInfo!
var intValue: UInt = 0
(info[AVAudioSessionInterruptionTypeKey] as NSValue).getValue(&intValue)
if let type = AVAudioSessionInterruptionType.fromRaw(intValue) {
switch type {
case .Began:
// interruption began
println("began")
case .Ended:
// interruption ended
println("ended")
}
}
}
@onmyway133
Copy link

It doesn't call on when interruption ends

@mstultz
Copy link

mstultz commented Feb 16, 2018

interruption end events are not guaranteed:

Note: There is no guarantee that a begin interruption will have a corresponding end interruption. Your app needs to be aware of a switch to a foreground running state or the user pressing a Play button. In either case, determine whether your app should reactivate its audio session.

https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment