AVAudioPlayer在后台没有响应

当我的应用程序收到voip通知时,我在后台播放audio。 但AVAudioPlayerperformance怪异,有时会播放audio,有时并非没有任何错误。 多数民众赞成我是如何玩:

func playAudio() { // Set the sound file name & extension let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("testAudio", ofType: "wav")!) // Play the sound do { try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) audioPlayer.delegate = self if (audioPlayer.prepareToPlay()) { audioPlayer.play() } else {NSLog("there is error")} } catch { NSLog("there is \(error)") } } 

我在我的AppDelegate应用程序函数中初始化AVAudioSession:

  do { audioPlayer = AVAudioPlayer() audioSession = AVAudioSession() try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [AVAudioSessionCategoryOptions.MixWithOthers]) try audioSession.setActive(true) try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker) } catch { NSLog("there is \(error)") } 

当然还有var audioPlayer: AVAudioPlayer! 作为一个类动词

它的工作一两分钟,然后AVAudioPlayer停止响应没有任何错误。 我甚至尝试使用AVAudioPlayerDelegate来接收事件,但没有发生。

有人可以解释这种行为吗? 这是在ios中的错误? 或者我做错了什么? 在Ios 9.2,iPhone 6设备,Xcode 7.2上testing。

谢谢。