Fix: Always detach voice message audio player state before playing another one (#1965)

This commit is contained in:
Nicolas Mauri
2023-10-26 14:12:21 +02:00
committed by GitHub
parent a1bbc7ecce
commit 8a5986f9b0
@@ -263,6 +263,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
} }
func playPauseAudio(for itemID: TimelineItemIdentifier) async { func playPauseAudio(for itemID: TimelineItemIdentifier) async {
MXLog.info("Toggle play/pause audio for itemID \(itemID)")
guard let timelineItem = timelineItems.firstUsingStableID(itemID) else { guard let timelineItem = timelineItems.firstUsingStableID(itemID) else {
fatalError("TimelineItem \(itemID) not found") fatalError("TimelineItem \(itemID) not found")
} }
@@ -283,15 +284,19 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
let audioPlayerState = audioPlayerState(for: itemID) 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 { guard audioPlayer.mediaSource == source, audioPlayer.state != .error else {
audioPlayer.stop() audioPlayer.stop()
await mediaPlayerProvider.detachAllStates(except: audioPlayerState)
audioPlayerState.attachAudioPlayer(audioPlayer)
// Load content // Load content
do { do {
MXLog.info("Loading voice message audio content from source for itemID \(itemID)")
let url = try await voiceMessageMediaManager.loadVoiceMessageFromSource(source, body: nil) 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. // 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 { if audioPlayer.state == .playing {
audioPlayer.pause() audioPlayer.pause()
} else { } else {
audioPlayerState.attachAudioPlayer(audioPlayer)
audioPlayer.play() audioPlayer.play()
} }
} }