rename observeIncomingEvents method and reorder some methods
This commit is contained in:
parent
62c574b966
commit
838e11c167
@ -161,40 +161,15 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
|
|||||||
|
|
||||||
val voiceBroadcastState = getVoiceBroadcastUseCase.execute(roomId, eventId)?.content?.voiceBroadcastState
|
val voiceBroadcastState = getVoiceBroadcastUseCase.execute(roomId, eventId)?.content?.voiceBroadcastState
|
||||||
isLive = voiceBroadcastState != null && voiceBroadcastState != VoiceBroadcastState.STOPPED
|
isLive = voiceBroadcastState != null && voiceBroadcastState != VoiceBroadcastState.STOPPED
|
||||||
observeIncomingEvents(roomId, eventId)
|
fetchPlaylistAndStartPlayback(roomId, eventId)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startPlayback() {
|
private fun fetchPlaylistAndStartPlayback(roomId: String, voiceBroadcastId: String) {
|
||||||
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) {
|
|
||||||
fetchPlaylistJob = getLiveVoiceBroadcastChunksUseCase.execute(roomId, voiceBroadcastId)
|
fetchPlaylistJob = getLiveVoiceBroadcastChunksUseCase.execute(roomId, voiceBroadcastId)
|
||||||
.onEach(this::updatePlaylist)
|
.onEach(this::updatePlaylist)
|
||||||
.launchIn(coroutineScope)
|
.launchIn(coroutineScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resumePlayback() {
|
|
||||||
currentMediaPlayer?.start()
|
|
||||||
currentVoiceBroadcastId?.let { playbackTracker.startPlayback(it) }
|
|
||||||
playingState = State.PLAYING
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updatePlaylist(playlist: List<MessageAudioEvent>) {
|
private fun updatePlaylist(playlist: List<MessageAudioEvent>) {
|
||||||
this.playlist = playlist.sortedBy { it.getVoiceBroadcastChunk()?.sequence?.toLong() ?: it.root.originServerTs }
|
this.playlist = playlist.sortedBy { it.getVoiceBroadcastChunk()?.sequence?.toLong() ?: it.root.originServerTs }
|
||||||
onPlaylistUpdated()
|
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? {
|
private fun getNextAudioContent(): MessageAudioContent? {
|
||||||
val nextSequence = currentSequence?.plus(1)
|
val nextSequence = currentSequence?.plus(1)
|
||||||
?: playlist.lastOrNull()?.sequence
|
?: playlist.lastOrNull()?.sequence
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user