From 838e11c167a5bdf9d4b36ef1221f0518029a94b8 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Fri, 28 Oct 2022 10:43:05 +0200 Subject: [PATCH] rename observeIncomingEvents method and reorder some methods --- .../listening/VoiceBroadcastPlayerImpl.kt | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt index 9afe428e59..3999a0e0af 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt @@ -161,40 +161,15 @@ class VoiceBroadcastPlayerImpl @Inject constructor( val voiceBroadcastState = getVoiceBroadcastUseCase.execute(roomId, eventId)?.content?.voiceBroadcastState isLive = voiceBroadcastState != null && voiceBroadcastState != VoiceBroadcastState.STOPPED - observeIncomingEvents(roomId, eventId) + fetchPlaylistAndStartPlayback(roomId, eventId) } - private fun startPlayback() { - val event = if (isLive) playlist.lastOrNull() else playlist.firstOrNull() - val content = event?.content ?: run { Timber.w("## VoiceBroadcastPlayer: No content to play"); return } - val sequence = event.getVoiceBroadcastChunk()?.sequence - coroutineScope.launch { - try { - currentMediaPlayer = prepareMediaPlayer(content) - currentMediaPlayer?.start() - currentVoiceBroadcastId?.let { playbackTracker.startPlayback(it) } - currentSequence = sequence - withContext(Dispatchers.Main) { playingState = State.PLAYING } - nextMediaPlayer = prepareNextMediaPlayer() - } catch (failure: Throwable) { - Timber.e(failure, "Unable to start playback") - throw VoiceFailure.UnableToPlay(failure) - } - } - } - - private fun observeIncomingEvents(roomId: String, voiceBroadcastId: String) { + private fun fetchPlaylistAndStartPlayback(roomId: String, voiceBroadcastId: String) { fetchPlaylistJob = getLiveVoiceBroadcastChunksUseCase.execute(roomId, voiceBroadcastId) .onEach(this::updatePlaylist) .launchIn(coroutineScope) } - private fun resumePlayback() { - currentMediaPlayer?.start() - currentVoiceBroadcastId?.let { playbackTracker.startPlayback(it) } - playingState = State.PLAYING - } - private fun updatePlaylist(playlist: List) { this.playlist = playlist.sortedBy { it.getVoiceBroadcastChunk()?.sequence?.toLong() ?: it.root.originServerTs } onPlaylistUpdated() @@ -220,6 +195,31 @@ class VoiceBroadcastPlayerImpl @Inject constructor( } } + private fun startPlayback() { + val event = if (isLive) playlist.lastOrNull() else playlist.firstOrNull() + val content = event?.content ?: run { Timber.w("## VoiceBroadcastPlayer: No content to play"); return } + val sequence = event.getVoiceBroadcastChunk()?.sequence + coroutineScope.launch { + try { + currentMediaPlayer = prepareMediaPlayer(content) + currentMediaPlayer?.start() + currentVoiceBroadcastId?.let { playbackTracker.startPlayback(it) } + currentSequence = sequence + withContext(Dispatchers.Main) { playingState = State.PLAYING } + nextMediaPlayer = prepareNextMediaPlayer() + } catch (failure: Throwable) { + Timber.e(failure, "Unable to start playback") + throw VoiceFailure.UnableToPlay(failure) + } + } + } + + private fun resumePlayback() { + currentMediaPlayer?.start() + currentVoiceBroadcastId?.let { playbackTracker.startPlayback(it) } + playingState = State.PLAYING + } + private fun getNextAudioContent(): MessageAudioContent? { val nextSequence = currentSequence?.plus(1) ?: playlist.lastOrNull()?.sequence