I am developing a game that includes a background audio player running in the background. When you deal with pausing all tracks, and then resuming when you press the pause button, I did not find any significant difficulties:
class AudioMultiTrack {
var tracks : [AudioTrack]
var mixer : AVAudioMixerNode
var pauseTime : AVAudioTime?
var engine = AVAudioEngine()
var volumes = [Float]()
init(audioTracks: [AudioTrack]){
tracks = audioTracks
mixer = engine.mainMixerNode
}
func initialize() {
for track in tracks {
track.setUpTrack()
engine.attach(track.audioPlayer)
engine.connect(track.audioPlayer, to: mixer, format: track.format)
track.scheduleFile()
}
do {
try engine.start()
} catch {
fatalError("error starting engine")
}
}
func play() {
for track in tracks {
track.audioPlayer.play(at: startTime())
}
}
func pause() {
pauseTime = tracks[0].audioPlayer.lastRenderTime
for track in tracks {
track.audioPlayer.pause()
}
}
func unPause() {
for track in tracks {
track.scheduleFile()
track.audioPlayer.play(at: pauseTime)
}
}
func stop() {
pauseTime = tracks[0].audioPlayer.lastRenderTime
engine.stop()
for track in tracks {
engine.detach(track.audioPlayer)
}
engine.reset()
}
func startTime() ->AVAudioTime {
let sampleRate = tracks[0].format.sampleRate
let sampleTime = AVAudioFramePosition(sampleRate)
return AVAudioTime(sampleTime: sampleTime, atRate: sampleRate)
}
When a pause is pressed in the GameViewController:
func pausePressed() {
audio.pause()
}
func continuePressed() {
audio.unPause()
}
However, when considering interruptions or route changes in this way:
func handleInterruption(notification: NSNotification) {
guard let info = notification.userInfo else { return }
switch info[AVAudioSessionInterruptionTypeKey] as! Int {
case 0:
audio.unPause()
case 1:
audio.pause()
default:
break;
}
}
The program crashes, and I get:
*** Application termination due to the uncaught exception "com.apple.coreaudio.avfaudio", reason: "prerequisite is false: _engine-> IsRunning ()"
So, it looks like the sound engine is stopping, and I don't want to reinitialize the audio player for a number of reasons, including setup time and game continuity ...
? , ?