Fixed ANR related to voice record player + other related improvements

This commit is contained in:
Sylvain Berfini 2023-11-13 12:14:26 +01:00
parent e0d07c80ac
commit 6aa2f49321
2 changed files with 20 additions and 9 deletions

View file

@ -33,11 +33,11 @@ import java.util.regex.Pattern
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.core.Address
@ -210,9 +210,15 @@ class ChatMessageModel @WorkerThread constructor(
displayableContentFound = true
}
"audio" -> {
isVoiceRecord.postValue(true)
voiceRecordPath = path
initVoiceRecordPlayer()
isVoiceRecord.postValue(true)
val duration = content.fileDuration
voiceRecordingDuration.postValue(duration)
val formattedDuration = SimpleDateFormat(
"mm:ss",
Locale.getDefault()
).format(duration) // duration is in ms
formattedVoiceRecordingDuration.postValue(formattedDuration)
displayableContentFound = true
}
else -> {
@ -239,6 +245,13 @@ class ChatMessageModel @WorkerThread constructor(
@WorkerThread
fun destroy() {
scope.cancel()
if (::voiceRecordPlayer.isInitialized) {
stopVoiceRecordPlayer()
voiceRecordPlayer.removeListener(playerListener)
}
chatMessage.removeListener(chatMessageListener)
}
@ -486,8 +499,8 @@ class ChatMessageModel @WorkerThread constructor(
voiceRecordPlayer.start()
playerTickerFlow().onEach {
withContext(Dispatchers.Main) {
voiceRecordPlayerPosition.value = voiceRecordPlayer.currentPosition
coreContext.postOnCoreThread {
voiceRecordPlayerPosition.postValue(voiceRecordPlayer.currentPosition)
}
}.launchIn(scope)
}

View file

@ -30,13 +30,11 @@ import androidx.lifecycle.viewModelScope
import androidx.media.AudioFocusRequestCompat
import java.text.SimpleDateFormat
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.core.ChatMessage
@ -553,8 +551,8 @@ class SendMessageInConversationViewModel @UiThread constructor() : ViewModel() {
isPlayingVoiceRecord.postValue(true)
playerTickerFlow().onEach {
withContext(Dispatchers.Main) {
voiceRecordPlayerPosition.value = voiceRecordPlayer.currentPosition
coreContext.postOnCoreThread {
voiceRecordPlayerPosition.postValue(voiceRecordPlayer.currentPosition)
}
}.launchIn(viewModelScope)
}