Swift - 当 AVPlayer 在后台模式下被覆盖时,如何发送通知?
Swift - How can I send a notification when AVPlayer gets overridden in the background mode?
即使应用程序处于后台,我的应用程序也会继续播放音乐,我希望在 AVPlayer
被覆盖时向用户发送通知(例如,如果用户使用另一个应用程序覆盖它).
目前我的解决方案是使用设置 notification
的 checkInTimer
,如果应用程序在 x 时间后未签入,则 notification
关闭,但是如果确实签入,它会删除通知并设置另一个通知。但是这个解决方案很烂..
有什么想法吗?
你需要观察audio interruptions:
import AVFoundation
func setup() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(myInterruptionHandler), name: AVAudioSessionInterruptionNotification, object: nil)
}
func myInterruptionHandler(notification: NSNotification) {
var info = notification.userInfo!
var intValue: UInt = 0
(info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)
if let type = AVAudioSessionInterruptionType(rawValue: intValue) {
switch type {
case .Began:
// interruption began
case .Ended:
// interruption ended
}
}
}
即使应用程序处于后台,我的应用程序也会继续播放音乐,我希望在 AVPlayer
被覆盖时向用户发送通知(例如,如果用户使用另一个应用程序覆盖它).
目前我的解决方案是使用设置 notification
的 checkInTimer
,如果应用程序在 x 时间后未签入,则 notification
关闭,但是如果确实签入,它会删除通知并设置另一个通知。但是这个解决方案很烂..
有什么想法吗?
你需要观察audio interruptions:
import AVFoundation
func setup() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(myInterruptionHandler), name: AVAudioSessionInterruptionNotification, object: nil)
}
func myInterruptionHandler(notification: NSNotification) {
var info = notification.userInfo!
var intValue: UInt = 0
(info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)
if let type = AVAudioSessionInterruptionType(rawValue: intValue) {
switch type {
case .Began:
// interruption began
case .Ended:
// interruption ended
}
}
}