[Android] [PATCH 2/3] Fix AudioService -> VideoPlayer transition

Thomas Guillem thomas at gllm.fr
Thu Apr 9 17:45:49 CEST 2015


This partially reverts commit 80f7b8ae3f857b694a55074d2c2436292e715348 "Always
Open stream from VideoPlayerActivity"
---
 .../src/org/videolan/vlc/audio/AudioService.java   | 22 ++++++
 .../vlc/gui/video/VideoPlayerActivity.java         | 84 +++++++++++++++-------
 2 files changed, 82 insertions(+), 24 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
index c09244d..9c553fa 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
@@ -71,6 +71,7 @@ import org.videolan.vlc.RemoteControlClientReceiver;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.gui.audio.AudioUtil;
+import org.videolan.vlc.gui.video.VideoPlayerActivity;
 import org.videolan.vlc.interfaces.IAudioService;
 import org.videolan.vlc.interfaces.IAudioServiceCallback;
 import org.videolan.vlc.util.Util;
@@ -549,6 +550,9 @@ public class AudioService extends Service {
                     service.showNotification();
                     service.updateRemoteControlClientMetadata();
                     break;
+                case EventHandler.MediaPlayerESAdded:
+                    service.handleVout();
+                    break;
                 default:
                     Log.e(TAG, String.format("Event not handled (0x%x)", msg.getData().getInt("event")));
                     break;
@@ -613,6 +617,24 @@ public class AudioService extends Service {
         }
     };
 
+    private void handleVout() {
+        if (mLibVLC.getVideoTracksCount() <= 0 || !hasCurrentMedia())
+            return;
+        final MediaWrapper mw = mMediaListPlayer.getMediaList().getMedia(mCurrentIndex);
+        if (mw == null || (mw.getFlags() & LibVLC.MEDIA_NO_VIDEO) != 0)
+            return;
+
+        Log.i(TAG, "Obtained video track");
+        int index = mCurrentIndex;
+        mCurrentIndex = -1;
+        mEventHandler.removeHandler(mVlcEventHandler);
+        // Preserve playback when switching to video
+        hideNotification(false);
+
+        // Switch to the video player & don't lose the currently playing stream
+        VideoPlayerActivity.startOpened(VLCApplication.getAppContext(), index);
+    }
+
     private void executeUpdate() {
         executeUpdate(true);
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 84852f7..af5af1f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -147,6 +147,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     public final static String PLAY_EXTRA_SUBTITLES_LOCATION = "subtitles_location";
     public final static String PLAY_EXTRA_ITEM_TITLE = "item_title";
     public final static String PLAY_EXTRA_FROM_START = "from_start";
+    public final static String PLAY_EXTRA_OPENED_POSITION = "opened_position";
 
     private SurfaceView mSurfaceView;
     private SurfaceView mSubtitlesSurfaceView;
@@ -802,25 +803,29 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     }
 
     public static void start(Context context, String location) {
-        start(context, location, null, false, false);
+        start(context, location, null, false, -1);
     }
 
     public static void start(Context context, String location, boolean fromStart) {
-        start(context, location, null, fromStart, false);
+        start(context, location, null, fromStart, -1);
     }
 
     public static void start(Context context, String location, String title) {
-        start(context, location, title, false, false);
+        start(context, location, title, false, -1);
+    }
+    public static void startOpened(Context context, int openedPosition) {
+        start(context, null, null, false, openedPosition);
     }
 
-    public static void start(Context context, String location, String title, boolean fromStart, boolean newTask) {
+    private static void start(Context context, String location, String title, boolean fromStart, int openedPosition) {
         Intent intent = new Intent(context, VideoPlayerActivity.class);
         intent.setAction(VideoPlayerActivity.PLAY_FROM_VIDEOGRID);
         intent.putExtra(PLAY_EXTRA_ITEM_LOCATION, location);
         intent.putExtra(PLAY_EXTRA_ITEM_TITLE, title);
         intent.putExtra(PLAY_EXTRA_FROM_START, fromStart);
+        intent.putExtra(PLAY_EXTRA_OPENED_POSITION, openedPosition);
 
-        if (newTask)
+        if (openedPosition != -1)
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
 
         context.startActivity(intent);
@@ -1415,11 +1420,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                     break;
                 case EventHandler.MediaPlayerPlaying:
                     Log.i(TAG, "MediaPlayerPlaying");
-                    activity.stopLoadingAnimation();
-                    activity.showOverlay();
-                    activity.setESTracks();
-                    activity.changeAudioFocus(true);
-                    activity.updateNavStatus();
+                    activity.onPlaying();
                     break;
                 case EventHandler.MediaPlayerPaused:
                     Log.i(TAG, "MediaPlayerPaused");
@@ -1521,6 +1522,14 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         return !mDragging && mShowing && mLibVLC.isPlaying();
     }
 
+    private void onPlaying() {
+        stopLoadingAnimation();
+        showOverlay();
+        setESTracks();
+        changeAudioFocus(true);
+        updateNavStatus();
+    }
+
     private void endReached() {
         if(mMediaListPlayer.expand(savedIndexPosition) == 0) {
             Log.d(TAG, "Found a video playlist, expanding it");
@@ -2567,6 +2576,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         mLocation = null;
         String title = getResources().getString(R.string.title);
         boolean fromStart = false;
+        int openedPosition = -1;
         Uri data;
         String itemTitle = null;
         long intentPosition = -1; // position passed in by intent (ms)
@@ -2687,7 +2697,9 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             if (getIntent().hasExtra(PLAY_EXTRA_SUBTITLES_LOCATION))
                 mSubtitleSelectedFiles.add(getIntent().getExtras().getString(PLAY_EXTRA_SUBTITLES_LOCATION));
             mAskResume &= !fromStart;
+            openedPosition = getIntent().getExtras().getInt(PLAY_EXTRA_OPENED_POSITION);
         }
+        final boolean opened = openedPosition != -1;
 
         /* WARNING: hack to avoid a crash in mediacodec on KitKat.
          * Disable hardware acceleration if the media has a ts extension. */
@@ -2704,15 +2716,29 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             }
         }
 
-        /* prepare playback */
-        AudioServiceController.getInstance().stop(); // Stop the previous playback.
-        if (savedIndexPosition == -1 && mLocation != null && mLocation.length() > 0) {
-            mMediaListPlayer.getMediaList().clear();
-            final Media media = new Media(mLibVLC, mLocation);
-            media.parse(); // FIXME: parse should'nt be done asynchronously
-            media.release();
-            mMediaListPlayer.getMediaList().add(new MediaWrapper(media));
-            savedIndexPosition = mMediaListPlayer.getMediaList().size() - 1;
+        /* Start / resume playback */
+        if (opened) {
+            // Provided externally from AudioService
+            Log.d(TAG, "Continuing playback from AudioService at index " + openedPosition);
+            MediaWrapper openedMedia = mMediaListPlayer.getMediaList().getMedia(openedPosition);
+            if (openedMedia == null) {
+                encounteredError();
+                return;
+            }
+            mLocation = openedMedia.getLocation();
+            itemTitle = openedMedia.getTitle();
+            savedIndexPosition = openedPosition;
+        } else {
+            /* prepare playback */
+            AudioServiceController.getInstance().stop(); // Stop the previous playback.
+            if (savedIndexPosition == -1 && mLocation != null && mLocation.length() > 0) {
+                mMediaListPlayer.getMediaList().clear();
+                final Media media = new Media(mLibVLC, mLocation);
+                media.parse(); // FIXME: parse should'nt be done asynchronously
+                media.release();
+                mMediaListPlayer.getMediaList().add(new MediaWrapper(media));
+                savedIndexPosition = mMediaListPlayer.getMediaList().size() - 1;
+            }
         }
         mCanSeek = false;
 
@@ -2721,7 +2747,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             MediaWrapper media = MediaDatabase.getInstance().getMedia(mLocation);
             if(media != null) {
                 // in media library
-                if(media.getTime() > 0 && !fromStart) {
+                if(media.getTime() > 0 && !fromStart && !opened) {
                     if (mAskResume) {
                         showConfirmResumeDialog();
                         return;
@@ -2736,7 +2762,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
 
                 mLastAudioTrack = media.getAudioTrack();
                 mLastSpuTrack = media.getSpuTrack();
-            } else {
+            } else if (!opened) {
                 // not in media library
 
                 if (intentPosition > 0 && mAskResume) {
@@ -2759,9 +2785,19 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             }
 
             // Start playback & seek
-            VLCInstance.setAudioHdmiEnabled(this, mHasHdmiAudio);
-            mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
-            seek(intentPosition, mediaLength);
+            if (!opened) {
+                VLCInstance.setAudioHdmiEnabled(this, mHasHdmiAudio);
+                mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
+                seek(intentPosition, mediaLength);
+            } else {
+                mLibVLC.setVideoTrack(-1);
+                mLibVLC.setVideoTrack(0);
+                // AudioService-transitioned playback for item after sleep and resume
+                if(!mLibVLC.isPlaying())
+                    mMediaListPlayer.playIndex(savedIndexPosition);
+                else
+                    onPlaying();
+            }
 
 
             // Get possible subtitles
-- 
2.1.3



More information about the Android mailing list