Prevent crash if MediaPlayer can't be instancianted

This commit is contained in:
Sylvain Berfini 2025-02-18 16:58:28 +01:00
parent ef79475525
commit 0bf6d60570
3 changed files with 35 additions and 25 deletions

View file

@ -34,6 +34,7 @@ import org.linphone.ui.GenericViewModel
import org.linphone.utils.Event
import org.linphone.utils.FileUtils
import org.linphone.utils.TimestampUtils
import org.linphone.R
class MediaViewModel
@UiThread
@ -163,33 +164,40 @@ class MediaViewModel
private fun initMediaPlayer() {
isMediaPlaying.value = false
mediaPlayer = MediaPlayer().apply {
setAudioAttributes(
AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).setUsage(
AudioAttributes.USAGE_MEDIA
).build()
)
setDataSource(filePath)
setOnCompletionListener {
Log.i("$TAG Media player reached the end of file")
isMediaPlaying.postValue(false)
position.postValue(0)
stopUpdatePlaybackPosition()
try {
mediaPlayer = MediaPlayer().apply {
setAudioAttributes(
AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(
AudioAttributes.USAGE_MEDIA
).build()
)
setDataSource(filePath)
setOnCompletionListener {
Log.i("$TAG Media player reached the end of file")
isMediaPlaying.postValue(false)
position.postValue(0)
stopUpdatePlaybackPosition()
// Leave full screen when playback is done
fullScreenMode.postValue(false)
changeFullScreenModeEvent.postValue(Event(false))
}
setOnVideoSizeChangedListener { mediaPlayer, width, height ->
videoSizeChangedEvent.postValue(Event(Pair(width, height)))
}
try {
prepare()
} catch (e: Exception) {
fullScreenMode.postValue(false)
changeFullScreenModeEvent.postValue(Event(false))
Log.e("$TAG Failed to prepare video file: $e")
// Leave full screen when playback is done
fullScreenMode.postValue(false)
changeFullScreenModeEvent.postValue(Event(false))
}
setOnVideoSizeChangedListener { mediaPlayer, width, height ->
videoSizeChangedEvent.postValue(Event(Pair(width, height)))
}
try {
prepare()
} catch (e: Exception) {
fullScreenMode.postValue(false)
changeFullScreenModeEvent.postValue(Event(false))
Log.e("$TAG Failed to prepare video file: $e")
}
}
} catch (e: Exception) {
Log.e("$TAG Failed to initialize media player for file [$filePath]: $e")
showRedToast(R.string.media_player_generic_error_toast, R.drawable.warning_circle)
return
}
val durationInMillis = mediaPlayer.duration

View file

@ -90,6 +90,7 @@
<string name="media_playback_low_volume_warning_toast">Volume faible : vous risquez de ne pas entendre</string>
<string name="remote_provisioning_config_applied_toast">Configuration appliquée</string>
<string name="remote_provisioning_config_failed_toast">Erreur lors du chargement ou de l\'application de la configuration</string>
<string name="media_player_generic_error_toast">Erreur lors de la création du lecteur média</string>
<!-- Assistant related string (account register / login / etc...) -->
<string name="assistant_dialog_general_terms_and_privacy_policy_title">Conditions de service &amp; politique de confidentialité</string>

View file

@ -129,6 +129,7 @@
<string name="media_playback_low_volume_warning_toast">Media volume is low, you may not hear anything!</string>
<string name="remote_provisioning_config_applied_toast">Configuration successfully applied</string>
<string name="remote_provisioning_config_failed_toast">Error while trying to download and apply remote configuration</string>
<string name="media_player_generic_error_toast">Error trying to create media player</string>
<!-- Assistant related string (account register / login / etc...) -->
<string name="assistant_dialog_general_terms_and_privacy_policy_title">General terms &amp; privacy policy</string>