From 8a5986f9b0d2ba90e6ebff37ca63bca64babd4c2 Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Thu, 26 Oct 2023 14:12:21 +0200 Subject: [PATCH] Fix: Always detach voice message audio player state before playing another one (#1965) --- .../RoomTimelineController.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ElementX/Sources/Services/Timeline/TimelineController/RoomTimelineController.swift b/ElementX/Sources/Services/Timeline/TimelineController/RoomTimelineController.swift index 7d03573f5..f874396cf 100644 --- a/ElementX/Sources/Services/Timeline/TimelineController/RoomTimelineController.swift +++ b/ElementX/Sources/Services/Timeline/TimelineController/RoomTimelineController.swift @@ -263,6 +263,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol { } func playPauseAudio(for itemID: TimelineItemIdentifier) async { + MXLog.info("Toggle play/pause audio for itemID \(itemID)") guard let timelineItem = timelineItems.firstUsingStableID(itemID) else { fatalError("TimelineItem \(itemID) not found") } @@ -283,15 +284,19 @@ class RoomTimelineController: RoomTimelineControllerProtocol { let audioPlayerState = audioPlayerState(for: itemID) + // Detach all other states + await mediaPlayerProvider.detachAllStates(except: audioPlayerState) + // Ensure this one is attached + if !audioPlayerState.isAttached { + audioPlayerState.attachAudioPlayer(audioPlayer) + } + guard audioPlayer.mediaSource == source, audioPlayer.state != .error else { audioPlayer.stop() - await mediaPlayerProvider.detachAllStates(except: audioPlayerState) - - audioPlayerState.attachAudioPlayer(audioPlayer) - // Load content do { + MXLog.info("Loading voice message audio content from source for itemID \(itemID)") let url = try await voiceMessageMediaManager.loadVoiceMessageFromSource(source, body: nil) // Make sure that the player is still attached, as it may have been detached while waiting for the voice message to be loaded. @@ -309,7 +314,6 @@ class RoomTimelineController: RoomTimelineControllerProtocol { if audioPlayer.state == .playing { audioPlayer.pause() } else { - audioPlayerState.attachAudioPlayer(audioPlayer) audioPlayer.play() } }