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

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


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

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

(cherry picked from commit 372a13d4912632ffd0ff144f056de76216a0a5b8)

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

 .../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 3846aa7f0..011fe0471 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -126,6 +126,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";
@@ -357,6 +358,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
     @Override
     public void onDestroy() {
         super.onDestroy();
+        mHandler.removeCallbacksAndMessages(null);
         stop(true);
         if (mMediaSession != null) {
             mMediaSession.release();
@@ -945,7 +947,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) {
@@ -962,6 +963,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;
             }
         }
     }
@@ -1449,10 +1454,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;
@@ -1461,9 +1468,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