rename observeIncomingEvents method and reorder some methods

This commit is contained in:
Florian Renaud 2022-10-28 10:43:05 +02:00
parent 62c574b966
commit 838e11c167
1 changed files with 27 additions and 27 deletions

View File

@ -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<MessageAudioEvent>) {
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