[Android] [PATCH 3/3] VideoPlayer: start playback when surfaces are ready

Thomas Guillem thomas at gllm.fr
Wed Apr 8 17:30:18 CEST 2015


The eventVideoPlayerActivityCreated hack is not needed anymore. Vlc will fails
immediatly and won't block if no surfaces are available.
---
 .../vlc/gui/video/VideoPlayerActivity.java         | 27 ++++++++++++++++++----
 1 file changed, 22 insertions(+), 5 deletions(-)

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 64ecba6..580b10e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -186,7 +186,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     private static final int SHOW_PROGRESS = 2;
     private static final int SURFACE_LAYOUT = 3;
     private static final int FADE_OUT_INFO = 4;
-    private static final int AUDIO_SERVICE_CONNECTION_SUCCESS = 5;
+    private static final int START_PLAYBACK = 5;
     private static final int AUDIO_SERVICE_CONNECTION_FAILED = 6;
     private static final int END_DELAY_STATE = 7;
     private static final int RESET_BACK_LOCK = 8;
@@ -311,6 +311,10 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     private OnLayoutChangeListener mOnLayoutChangeListener;
     private AlertDialog mAlertDialog;
 
+    private boolean mAudioServiceReady = false;
+    private boolean mSurfaceReady = false;
+    private boolean mSubtitleSurfaceReady = false;
+
     private boolean mHasHdmiAudio = false;
 
     @Override
@@ -438,8 +442,10 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         mSubtitlesSurfaceView.setZOrderMediaOverlay(true);
         mSubtitlesSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
 
-        if (mLibVLC.useCompatSurface())
+        if (mLibVLC.useCompatSurface()) {
             mSubtitlesSurfaceView.setVisibility(View.GONE);
+            mSubtitleSurfaceReady = true;
+        }
         if (mPresentation == null) {
             mSurfaceHolder.addCallback(mSurfaceCallback);
             mSubtitlesSurfaceHolder.addCallback(mSubtitlesSurfaceCallback);
@@ -600,18 +606,21 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                 new AudioServiceController.AudioServiceConnectionListener() {
                     @Override
                     public void onConnectionSuccess() {
-                        mHandler.sendEmptyMessage(AUDIO_SERVICE_CONNECTION_SUCCESS);
+                        mAudioServiceReady = true;
+                        mHandler.sendEmptyMessage(START_PLAYBACK);
                     }
 
                     @Override
                     public void onConnectionFailed() {
                         mBound = false;
+                        mAudioServiceReady = false;
                         mHandler.sendEmptyMessage(AUDIO_SERVICE_CONNECTION_FAILED);
                     }
                 });
     }
     private void unbindAudioService() {
         AudioServiceController.getInstance().unbindAudioService(this);
+        mAudioServiceReady = false;
         mBound = false;
     }
 
@@ -642,8 +651,10 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     }
 
     private void startPlayback() {
-        if (mPlaybackStarted)
+        /* start playback only when audio service and both surfaces are ready */
+        if (mPlaybackStarted || !mAudioServiceReady || !mSurfaceReady || !mSubtitleSurfaceReady)
             return;
+
         mPlaybackStarted = true;
 
         if (LibVlcUtil.isHoneycombOrLater()) {
@@ -1485,7 +1496,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                 case FADE_OUT_INFO:
                     activity.fadeOutInfo();
                     break;
-                case AUDIO_SERVICE_CONNECTION_SUCCESS:
+                case START_PLAYBACK:
                     activity.startPlayback();
                     break;
                 case AUDIO_SERVICE_CONNECTION_FAILED:
@@ -2263,6 +2274,8 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                     mSurface = newSurface;
                     Log.d(TAG, "surfaceChanged: " + mSurface);
                     mLibVLC.attachSurface(mSurface, VideoPlayerActivity.this);
+                    mSurfaceReady = true;
+                    mHandler.sendEmptyMessage(START_PLAYBACK);
                 }
             }
         }
@@ -2277,6 +2290,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             if(mLibVLC != null) {
                 mSurface = null;
                 mLibVLC.detachSurface();
+                mSurfaceReady = false;
             }
         }
     };
@@ -2289,6 +2303,8 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                 if (mSubtitleSurface != newSurface) {
                     mSubtitleSurface = newSurface;
                     mLibVLC.attachSubtitlesSurface(mSubtitleSurface);
+                    mSubtitleSurfaceReady = true;
+                    mHandler.sendEmptyMessage(START_PLAYBACK);
                 }
             }
         }
@@ -2302,6 +2318,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             if(mLibVLC != null) {
                 mSubtitleSurface = null;
                 mLibVLC.detachSubtitlesSurface();
+                mSubtitleSurfaceReady = false;
             }
         }
     };
-- 
2.1.3



More information about the Android mailing list