【发布时间】:2017-05-31 14:05:36
【问题描述】:
我想在我的应用程序在后台模式下终止时播放声音。为此,我正在使用 VOIP 通知。我的方法是前台模式下的通话和声音播放。但是在后台模式下,方法调用声音不会触发。这是我的代码 sn-p。
func pushRegistry(registry: PKPushRegistry, didReceiveIncomingPushWithPayload payload: PKPushPayload, forType type: String) {
var a = payload.dictionaryPayload
// NSBundle.mainBundle().pathForResource(<#T##name: String?##String?#>, ofType: <#T##String?#>)
let url = NSBundle.mainBundle().URLForResource("demo", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOfURL: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}
}
【问题讨论】:
-
也许您应该在尝试播放声音之前配置 AVAudioSession?并配置你项目的能力(Target -> Capabilities -> Background Mode)
-
@AntonBelousov 它在前台模式下工作正常我们可以为后台模式做单独的代码