From 8c5fdb14a2781796db95cf05c835bd5969a8068b Mon Sep 17 00:00:00 2001 From: Olivier Bouillet Date: Thu, 24 Sep 2020 22:38:02 +0200 Subject: [PATCH] [Pip] : keep video aspect ratio for pip --- .../peertube/activity/VideoPlayActivity.java | 22 ++++++++++++------- .../fragment/VideoPlayerFragment.java | 13 +++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java index 398312f..ff2595a 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java @@ -451,15 +451,21 @@ public class VideoPlayActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.O) public void enterPipMode() { - Rational rational = new Rational(239, 100); - Log.v(TAG, rational.toString()); - PictureInPictureParams mParams = - new PictureInPictureParams.Builder() - .setAspectRatio(rational) -// .setSourceRectHint(new Rect(0,500,400,600)) - .build(); + final FragmentManager fragmentManager = getSupportFragmentManager(); + final VideoPlayerFragment videoPlayerFragment = (VideoPlayerFragment) fragmentManager.findFragmentById( R.id.video_player_fragment ); - enterPictureInPictureMode(mParams); + if ( videoPlayerFragment.getVideoAspectRatio() == 0 ) { + Log.i( TAG, "impossible to switch to pip" ); + } else { + Rational rational = new Rational( (int) ( videoPlayerFragment.getVideoAspectRatio() * 100 ), 100 ); + PictureInPictureParams mParams = + new PictureInPictureParams.Builder() + .setAspectRatio( rational ) +// .setSourceRectHint(new Rect(0,500,400,600)) + .build(); + + enterPictureInPictureMode( mParams ); + } } @Override diff --git a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java index 3ac32ba..6a19cc6 100644 --- a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java +++ b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java @@ -86,6 +86,7 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL private VideoPlayerService mService; private TorrentStream torrentStream; private LinearLayout torrentStatus; + private float aspectRatio; private static final String TAG = "VideoPlayerFragment"; private GestureDetector mDetector; @@ -112,6 +113,14 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL mBound = false; } }; + private AspectRatioFrameLayout.AspectRatioListener aspectRatioListerner = new AspectRatioFrameLayout.AspectRatioListener() + { + @Override + public void onAspectRatioUpdated( float targetAspectRatio, float naturalAspectRatio, boolean aspectRatioMismatch ) + { + aspectRatio = targetAspectRatio; + } + }; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @@ -144,6 +153,8 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL mDetector = new GestureDetector(context, new MyGestureListener()); simpleExoPlayerView.setOnTouchListener(touchListener); + simpleExoPlayerView.setAspectRatioListener( aspectRatioListerner ); + torrentStatus = activity.findViewById(R.id.exo_torrent_status); // Full screen Icon @@ -278,6 +289,8 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL } } + public float getVideoAspectRatio() { return aspectRatio; } + public boolean isPaused() { return !mService.player.getPlayWhenReady(); }