Fixes #3197 - Allow voice message playback in the background

- inform other audio sessions on deactivation
- let CallKit handle the audioSession for ElementCall
This commit is contained in:
Stefan Ceriu
2024-09-06 10:45:44 +03:00
committed by Stefan Ceriu
parent 5c9e13b200
commit 22bb305e79
3 changed files with 3 additions and 15 deletions

View File

@@ -133,7 +133,7 @@ class AudioPlayer: NSObject, AudioPlayerProtocol {
private func setupAudioSession() {
releaseAudioSessionTask = nil
do {
try audioSession.setCategory(AVAudioSession.Category.playback)
try audioSession.setCategory(.playback)
try audioSession.setActive(true)
} catch {
MXLog.error("Could not redirect audio playback to speakers.")
@@ -157,7 +157,7 @@ class AudioPlayer: NSObject, AudioPlayerProtocol {
releaseAudioSessionTask = nil
if audioSession.category == .playback, !audioSession.isOtherAudioPlaying {
MXLog.info("releasing audio session")
try? audioSession.setActive(false)
try? audioSession.setActive(false, options: .notifyOthersOnDeactivation)
}
}
@@ -209,16 +209,6 @@ class AudioPlayer: NSObject, AudioPlayerProtocol {
self.setInternalState(.finishedPlaying)
}
.store(in: &cancellables)
// Pause playback uppon UIApplication.didBecomeActiveNotification notification
NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)
.sink { [weak self] _ in
guard let self else { return }
self.pause()
// Release the audio session right away, as we don't play audio in the background
self.releaseAudioSession()
}
.store(in: &cancellables)
}
private func removeObservers() {

View File

@@ -119,7 +119,7 @@ class AudioRecorder: AudioRecorderProtocol {
private func releaseAudioSession() {
MXLog.info("releasing audio session")
try? audioSession.setActive(false)
try? audioSession.setActive(false, options: .notifyOthersOnDeactivation)
removeObservers()
}

View File

@@ -251,8 +251,6 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
// MARK: - Private
func tearDownCallSession(sendEndCallAction: Bool = true) {
try? AVAudioSession.sharedInstance().setActive(false)
if sendEndCallAction, let ongoingCallID {
let transaction = CXTransaction(action: CXEndCallAction(call: ongoingCallID.callKitID))
callController.request(transaction) { error in