Skip to content

Commit 32c103a

Browse files
committed
Fix alarm sound session activation failures in background
Use .duckOthers instead of empty options when configuring the audio session for alarm playback. The empty options created a non-mixable session that conflicted with the background silent audio player (which uses .mixWithOthers), causing setActive(true) to fail with "Session activation failed" when the app was in the background.
1 parent b588d5a commit 32c103a

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

LoopFollow/Controllers/AlarmSound.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class AlarmSound {
8888
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
8989
audioPlayer!.delegate = audioPlayerDelegate
9090

91-
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
91+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .duckOthers)
9292
try AVAudioSession.sharedInstance().setActive(true)
9393

9494
audioPlayer?.numberOfLoops = 0
@@ -126,7 +126,7 @@ class AlarmSound {
126126
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
127127
audioPlayer!.delegate = audioPlayerDelegate
128128

129-
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
129+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .duckOthers)
130130
try AVAudioSession.sharedInstance().setActive(true)
131131

132132
// Only use numberOfLoops if we're not using delay-based repeating
@@ -184,7 +184,7 @@ class AlarmSound {
184184
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
185185
audioPlayer!.delegate = audioPlayerDelegate
186186

187-
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
187+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .duckOthers)
188188
try AVAudioSession.sharedInstance().setActive(true)
189189

190190
audioPlayer!.numberOfLoops = 0
@@ -213,7 +213,7 @@ class AlarmSound {
213213
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
214214
audioPlayer!.delegate = audioPlayerDelegate
215215

216-
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
216+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .duckOthers)
217217
try AVAudioSession.sharedInstance().setActive(true)
218218

219219
// Play endless loops
@@ -262,7 +262,7 @@ class AlarmSound {
262262

263263
fileprivate static func enableAudio() {
264264
do {
265-
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
265+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .duckOthers)
266266
try AVAudioSession.sharedInstance().setActive(true)
267267
LogManager.shared.log(category: .alarm, message: "Audio session configured for alarm playback")
268268
} catch {

0 commit comments

Comments
 (0)