[Android] Release media player in a new thread + debug toast

Geoffrey Métais git at videolan.org
Tue Feb 6 11:58:25 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb  6 11:56:52 2018 +0100| [ae233f0c5e22b0c048fdb413405ef6797528e3a4] | committer: Geoffrey Métais

Release media player in a new thread + debug toast

In debug builds, if mediaplyer.release takes more than 5s, show a toast
to warn a potential deadlock

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

 .../src/org/videolan/vlc/media/PlayerController.kt      | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index 74b13678f..44c1a04cd 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -3,11 +3,12 @@ package org.videolan.vlc.media
 import android.net.Uri
 import android.support.annotation.MainThread
 import android.support.v4.media.session.PlaybackStateCompat
-import kotlinx.coroutines.experimental.async
-import kotlinx.coroutines.experimental.launch
-import kotlinx.coroutines.experimental.newSingleThreadContext
+import android.widget.Toast
+import kotlinx.coroutines.experimental.*
+import kotlinx.coroutines.experimental.android.UI
 import org.videolan.libvlc.*
 import org.videolan.medialibrary.media.MediaWrapper
+import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.RendererDelegate
 import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.gui.preferences.PreferencesActivity
@@ -158,7 +159,15 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
     fun release(player: MediaPlayer = mediaplayer) {
         player.setEventListener(null)
         if (isVideoPlaying()) player.vlcVout.detachViews()
-        launch(playerContext) { player.release() }
+        launch(newSingleThreadContext("vlc-player-release")) {
+            if (BuildConfig.DEBUG) { // Warn if player release is blocking
+                try {
+                    withTimeout(5000, { player.release() })
+                } catch (exception: TimeoutCancellationException) {
+                    launch(UI) { Toast.makeText(VLCApplication.getAppContext(), "media stop has timeouted!", Toast.LENGTH_LONG).show() }
+                }
+            } else player.release()
+        }
         playbackState = PlaybackStateCompat.STATE_STOPPED
     }
 



More information about the Android mailing list