[Android] TV: Pause mediasession for 15min if media is not ended

Geoffrey Métais git at videolan.org
Thu Nov 23 11:08:26 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Nov 23 09:42:32 2017 +0100| [73f1ed10264b89c4056fbf4c09897f9971b6e021] | committer: Geoffrey Métais

TV: Pause mediasession for 15min if media is not ended

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

 .../src/org/videolan/vlc/PlaybackService.java      | 24 ++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 8572df18b..5cdc5e1d7 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -125,6 +125,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
 
     private static final int SHOW_PROGRESS = 0;
     private static final int SHOW_TOAST = 1;
+    private static final int END_MEDIASESSION = 2;
     public static final String ACTION_REMOTE_GENERIC =  Strings.buildPkgString("remote.");
     public static final String ACTION_REMOTE_BACKWARD = ACTION_REMOTE_GENERIC+"Backward";
     public static final String ACTION_REMOTE_PLAY = ACTION_REMOTE_GENERIC+"Play";
@@ -355,6 +356,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
     @Override
     public void onDestroy() {
         super.onDestroy();
+        mHandler.removeCallbacksAndMessages(null);
         stop(true);
         if (mMediaSession != null) {
             mMediaSession.release();
@@ -943,7 +945,6 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
             final PlaybackService service = getOwner();
             if (service == null)
                 return;
-
             switch (msg.what) {
                 case SHOW_PROGRESS:
                     synchronized (service.mCallbacks) {
@@ -960,6 +961,10 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
                     final int duration = bundle.getInt("duration");
                     Toast.makeText(VLCApplication.getAppContext(), text, duration).show();
                     break;
+                case END_MEDIASESSION:
+                    if (service.mMediaSession != null)
+                        service.mMediaSession.setActive(false);
+                    break;
             }
         }
     }
@@ -1447,10 +1452,12 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
     protected void publishState() {
         if (mMediaSession == null)
             return;
+        if (AndroidDevices.isAndroidTv) mHandler.removeMessages(END_MEDIASESSION);
         final PlaybackStateCompat.Builder pscb = new PlaybackStateCompat.Builder();
         long actions = PLAYBACK_BASE_ACTIONS;
         final boolean hasMedia = hasCurrentMedia();
-        int state;
+        long time = getTime();
+        int state = PlaybackStateCompat.STATE_STOPPED;
         if (!mStopped && isPlaying()) {
             actions |= PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_STOP;
             state = PlaybackStateCompat.STATE_PLAYING;
@@ -1459,9 +1466,18 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
             state = PlaybackStateCompat.STATE_PAUSED;
         } else {
             actions |= PlaybackStateCompat.ACTION_PLAY;
-            state = AndroidDevices.isAndroidTv && hasMedia ? PlaybackStateCompat.STATE_PAUSED : PlaybackStateCompat.STATE_STOPPED;
+            final MediaWrapper media = AndroidDevices.isAndroidTv && hasMedia ? getCurrentMedia() : null;
+            if (media != null) {
+                final long length = media.getLength();
+                time = media.getTime();
+                float progress = length <= 0L ? 0f : time / (float)length;
+                if (progress < 0.95f) {
+                    state = PlaybackStateCompat.STATE_PAUSED;
+                    mHandler.sendEmptyMessageDelayed(END_MEDIASESSION, 900_000L);
+                }
+            }
         }
-        pscb.setState(state, getTime(), getRate());
+        pscb.setState(state, time, getRate());
         if (mRepeating != REPEAT_NONE || hasNext())
             actions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
         if (mRepeating != REPEAT_NONE || hasPrevious() || isSeekable())



More information about the Android mailing list