VoiceBroadcastPlayer - Use more accurate coroutine scope

This commit is contained in:
Florian Renaud 2022-10-19 13:16:54 +02:00
parent b89ab6c2fd
commit 0c847cffc1

View file

@ -24,6 +24,7 @@ import im.vector.app.features.home.room.detail.timeline.helper.AudioMessagePlayb
import im.vector.app.features.voice.VoiceFailure import im.vector.app.features.voice.VoiceFailure
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.events.model.RelationType import org.matrix.android.sdk.api.session.events.model.RelationType
@ -42,13 +43,13 @@ class VoiceBroadcastPlayer @Inject constructor(
private val sessionHolder: ActiveSessionHolder, private val sessionHolder: ActiveSessionHolder,
private val playbackTracker: AudioMessagePlaybackTracker, private val playbackTracker: AudioMessagePlaybackTracker,
) { ) {
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
private val session get() = sessionHolder.getActiveSession() private val session
get() = sessionHolder.getActiveSession()
private val mediaPlayerScope = CoroutineScope(Dispatchers.IO)
private var currentMediaPlayer: MediaPlayer? = null private var currentMediaPlayer: MediaPlayer? = null
private var currentPlayingIndex: Int = -1 private var currentPlayingIndex: Int = -1
private var playlist = emptyList<MessageAudioEvent>() private var playlist = emptyList<MessageAudioEvent>()
private val currentVoiceBroadcastEventId private val currentVoiceBroadcastEventId
get() = playlist.firstOrNull()?.root?.getRelationContent()?.eventId get() = playlist.firstOrNull()?.root?.getRelationContent()?.eventId
@ -90,7 +91,7 @@ class VoiceBroadcastPlayer @Inject constructor(
private fun startPlayback() { private fun startPlayback() {
val content = playlist.firstOrNull()?.content ?: run { Timber.w("## VoiceBroadcastPlayer: No content to play"); return } val content = playlist.firstOrNull()?.content ?: run { Timber.w("## VoiceBroadcastPlayer: No content to play"); return }
mediaPlayerScope.launch { coroutineScope.launch {
try { try {
currentMediaPlayer = prepareMediaPlayer(content) currentMediaPlayer = prepareMediaPlayer(content)
currentMediaPlayer?.start() currentMediaPlayer?.start()
@ -162,7 +163,7 @@ class VoiceBroadcastPlayer @Inject constructor(
release(currentMediaPlayer) release(currentMediaPlayer)
currentMediaPlayer = mp currentMediaPlayer = mp
currentPlayingIndex++ currentPlayingIndex++
mediaPlayerScope.launch { prepareNextFile() } coroutineScope.launch { prepareNextFile() }
} }
} }
return false return false