Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions LoopFollow/Controllers/AlarmSound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ class AlarmSound {
audioPlayer = nil

restoreSystemOutputVolume()
releaseAudioSession()
}

/// Hands the audio session back once LoopFollow has nothing left to play, so
/// audio from other apps that was ducked or interrupted returns to full volume.
/// While Silent Tune's loop holds the session, it stays active with the
/// mixable category that loop uses.
static func releaseAudioSession() {
guard !isPlaying else { return }

let session = AVAudioSession.sharedInstance()
do {
if MainViewController.shared?.backgroundTask.isPlaying == true {
try session.setCategory(.playback, mode: .default, options: .mixWithOthers)
} else {
try session.setActive(false, options: .notifyOthersOnDeactivation)
}
} catch {
let nsError = error as NSError
LogManager.shared.log(category: .alarm, message: "AlarmSound - audio session release failed [code \(nsError.code)]: \(error.localizedDescription)")
}
}

static func playTest() {
Expand Down Expand Up @@ -318,6 +339,7 @@ class AudioPlayerDelegate: NSObject, AVAudioPlayerDelegate {
AlarmSound.playNextWithDelay()
} else {
Observable.shared.alarmSoundPlaying.value = false
AlarmSound.releaseAudioSession()
}
}

Expand Down
14 changes: 1 addition & 13 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1266,18 +1266,6 @@ class MainViewController: UIViewController, UNUserNotificationCenterDelegate {

extension MainViewController: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_: AVSpeechSynthesizer, didFinish _: AVSpeechUtterance) {
let appState = UIApplication.shared.applicationState
let isSilentTuneMode = Storage.shared.backgroundRefreshType.value == .silentTune

if isSilentTuneMode, appState == .background {
LogManager.shared.log(category: .general, message: "Silent tune active in background; not deactivating session.", isDebug: true)
} else {
do {
try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
LogManager.shared.log(category: .general, message: "Audio session deactivated after speech.", isDebug: true)
} catch {
LogManager.shared.log(category: .alarm, message: "Failed to deactivate audio session: \(error)")
}
}
AlarmSound.releaseAudioSession()
}
}