[Android] Restart mediaplayer on error

Geoffrey Métais git at videolan.org
Fri Jan 26 15:16:54 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 25 17:20:21 2018 +0100| [dd65488e5c4fd08675febd4ba5fff4233ba8014a] | committer: Geoffrey Métais

Restart mediaplayer on error

> https://code.videolan.org/videolan/vlc-android/commit/dd65488e5c4fd08675febd4ba5fff4233ba8014a
---

 vlc-android/res/values/strings.xml                       |  1 +
 .../src/org/videolan/vlc/media/PlayerController.kt       | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index f1461abee..505e7d42a 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -586,4 +586,5 @@
     <string name="browser_storages">Storages</string>
     <string name="msg_delete_failed">Failed to delete media %1$s</string>
     <string name="renderer_list_title">Displays</string>
+    <string name="feedback_player_crashed">Sorry, VLC just crashed. Please try again.</string>
 </resources>
diff --git a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index e44b6fdd8..32fc73ce0 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -3,11 +3,15 @@ package org.videolan.vlc.media
 import android.net.Uri
 import android.support.annotation.MainThread
 import android.support.v4.media.session.PlaybackStateCompat
+import android.widget.Toast
+import kotlinx.coroutines.experimental.CoroutineExceptionHandler
+import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.async
 import kotlinx.coroutines.experimental.launch
 import kotlinx.coroutines.experimental.newSingleThreadContext
 import org.videolan.libvlc.*
 import org.videolan.medialibrary.media.MediaWrapper
+import org.videolan.vlc.R
 import org.videolan.vlc.RendererDelegate
 import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.gui.preferences.PreferencesActivity
@@ -17,6 +21,7 @@ import org.videolan.vlc.util.VLCOptions
 @Suppress("EXPERIMENTAL_FEATURE_WARNING")
 class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
 
+    private val exceptionHandler by lazy(LazyThreadSafetyMode.NONE) { CoroutineExceptionHandler { _, _ -> onPlayerError() } }
     private val playerContext by lazy(LazyThreadSafetyMode.NONE) { newSingleThreadContext("vlc-player") }
     private val settings by lazy(LazyThreadSafetyMode.NONE) { VLCApplication.getSettings() }
 
@@ -58,7 +63,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         mediaplayerEventListener = listener
         seekable = true
         pausable = true
-        launch(playerContext) {
+        launch(playerContext+exceptionHandler) {
             mediaplayer.media = media
         }.join()
         mediaplayer.setEventListener(this at PlayerController)
@@ -230,7 +235,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
     suspend fun expand(): MediaList? {
         return mediaplayer.media?.let {
             mediaplayer.setEventListener(null)
-            val ml = async { it.subItems() }.await()
+            val ml = async(playerContext+exceptionHandler) { it.subItems() }.await()
             it.release()
             mediaplayer.setEventListener(this at PlayerController)
             return ml
@@ -250,4 +255,11 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         }
         mediaplayerEventListener?.onEvent(event)
     }
+
+    private fun onPlayerError() {
+        launch(UI) {
+            restart()
+            Toast.makeText(VLCApplication.getAppContext(), VLCApplication.getAppContext().getString(R.string.feedback_player_crashed), Toast.LENGTH_LONG).show()
+        }
+    }
 }
\ No newline at end of file



More information about the Android mailing list