I have some music in my app that plays under AVAudioSessionCategoryAmbient. Right now my problem is that when a popup from Apple like "Sign in to iTunes Store" pops up it pauses my music, but does not resume it. Thankfully it takes care of my SKScene... but not the music. Is there some way to have a callback for when the alert is dismissed?
The weird thing is that other UIAlertViews that I have made don't do this to the app, so I really don't know what is up.
List of alerts that pause scene and music:
I had similar problems sometime ago (beginning of Swift 1.2). I solved my problem by observing paused status for SKScene. SKScene have view which is SKView type.
private var queueContext = 0
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.view.addObserver(self,
forKeyPath: "paused",
options: .New,
context: &queueContext)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &queueContext {
if let paused = change?[NSKeyValueChangeNewKey] as? Bool {
// TODO: react on paused status changes
}
}
else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
A few ideas:
Add a key-value observe on the player's rate property. When rate changes (the system UIAlertController stops the audio, start playback again.
For StoreKit transactions: I believe you can get and set callbacks for a successful or unsuccessful SKPaymentTransaction state. In these callbacks, start audio playback once more. This doesn't account for the low battery case though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With