proper audio interrupt and route change handling

- set AVAudioSession inactive on pause and stop
- handle audio route changes
This commit is contained in:
Toni Förster
2024-09-11 20:18:56 +02:00
parent 8596ee8811
commit a65ed67751
3 changed files with 143 additions and 27 deletions

View File

@@ -181,7 +181,9 @@ final class AVPlayerBackend: PlayerBackend {
{
seek(to: 0, seekType: .loopRestart)
}
#if !os(macOS)
model.setAudioSessionActive(true)
#endif
avPlayer.play()
// Setting hasStarted to true the first time player started
@@ -196,7 +198,9 @@ final class AVPlayerBackend: PlayerBackend {
guard avPlayer.timeControlStatus != .paused else {
return
}
#if !os(macOS)
model.setAudioSessionActive(false)
#endif
avPlayer.pause()
model.objectWillChange.send()
}
@@ -210,6 +214,9 @@ final class AVPlayerBackend: PlayerBackend {
}
func stop() {
#if !os(macOS)
model.setAudioSessionActive(false)
#endif
avPlayer.replaceCurrentItem(with: nil)
hasStarted = false
}
@@ -364,11 +371,7 @@ final class AVPlayerBackend: PlayerBackend {
let startPlaying = {
#if !os(macOS)
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
self.logger.error("Error setting up audio session: \(error)")
}
self.model.setAudioSessionActive(true)
#endif
self.setRate(self.model.currentRate)