From c8c3c23093bad7302acdfd587a1be2cd0290f851 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 14 Oct 2021 19:46:28 +0800 Subject: Zoomable ViewVideoActivity. --- app/build.gradle | 2 + .../activities/ViewVideoActivity.java | 46 ++++++++++++++++++---- app/src/main/res/layout/activity_view_video.xml | 16 ++++++-- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9049696f..0d3e20f7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -182,6 +182,8 @@ dependencies { // RecyclerView fast scrolling implementation 'me.zhanghai.android.fastscroll:library:1.1.7' + implementation 'com.otaliastudios:zoomlayout:1.8.0' + /**** Builds and flavors ****/ // debugImplementation because LeakCanary should only run in debug builds. diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java index 0942dbef..5a473d29 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -46,7 +46,7 @@ import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelectionArray; -import com.google.android.exoplayer2.ui.PlayerView; +import com.google.android.exoplayer2.ui.PlayerControlView; import com.google.android.exoplayer2.ui.TrackSelectionDialogBuilder; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; @@ -54,8 +54,10 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory; import com.google.android.exoplayer2.upstream.cache.SimpleCache; import com.google.android.exoplayer2.util.Util; +import com.google.android.exoplayer2.video.VideoListener; import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.snackbar.Snackbar; +import com.otaliastudios.zoom.ZoomSurfaceView; import org.apache.commons.io.FilenameUtils; @@ -131,8 +133,10 @@ public class ViewVideoActivity extends AppCompatActivity { CoordinatorLayout coordinatorLayout; @BindView(R.id.progress_bar_view_video_activity) ProgressBar progressBar; - @BindView(R.id.player_view_view_video_activity) - PlayerView videoPlayerView; + @BindView(R.id.zoom_surface_view_view_video_activity) + ZoomSurfaceView zoomSurfaceView; + @BindView(R.id.player_control_view_view_video_activity) + PlayerControlView playerControlView; @BindView(R.id.mute_exo_playback_control_view) ImageButton muteButton; @BindView(R.id.hd_exo_playback_control_view) @@ -325,7 +329,7 @@ public class ViewVideoActivity extends AppCompatActivity { } } - videoPlayerView.setControllerVisibilityListener(visibility -> { + playerControlView.setVisibilityListener(visibility -> { switch (visibility) { case View.GONE: getWindow().getDecorView().setSystemUiVisibility( @@ -347,7 +351,35 @@ public class ViewVideoActivity extends AppCompatActivity { TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(); trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); player = ExoPlayerFactory.newSimpleInstance(this, trackSelector); - videoPlayerView.setPlayer(player); + + playerControlView.setPlayer(player); + + player.addVideoListener(new VideoListener() { + @Override + public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { + zoomSurfaceView.setContentSize(width, height); + } + }); + zoomSurfaceView.addCallback(new ZoomSurfaceView.Callback() { + @Override + public void onZoomSurfaceCreated(@NonNull ZoomSurfaceView zoomSurfaceView) { + player.setVideoSurface(zoomSurfaceView.getSurface()); + } + + @Override + public void onZoomSurfaceDestroyed(@NonNull ZoomSurfaceView zoomSurfaceView) { + + } + }); + + zoomSurfaceView.setOnClickListener(view -> { + if (playerControlView.isVisible()) { + playerControlView.hide(); + } else { + playerControlView.show(); + } + }); + if (savedInstanceState == null) { mVideoUri = intent.getData(); videoType = getIntent().getIntExtra(EXTRA_VIDEO_TYPE, VIDEO_TYPE_NORMAL); @@ -507,8 +539,8 @@ public class ViewVideoActivity extends AppCompatActivity { dataSourceFactory = new CacheDataSourceFactory(mSimpleCache, new DefaultDataSourceFactory(ViewVideoActivity.this, Util.getUserAgent(ViewVideoActivity.this, "Infinity"))); - player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); preparePlayer(savedInstanceState); + player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); } @Override @@ -586,8 +618,8 @@ public class ViewVideoActivity extends AppCompatActivity { Util.getUserAgent(ViewVideoActivity.this, "Infinity"))); // Prepare the player with the source. - player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); preparePlayer(savedInstanceState); + player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); } else { Toast.makeText(ViewVideoActivity.this, R.string.error_fetching_v_redd_it_video_cannot_get_video_url, Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/res/layout/activity_view_video.xml b/app/src/main/res/layout/activity_view_video.xml index 85117f3f..bc828a7c 100644 --- a/app/src/main/res/layout/activity_view_video.xml +++ b/app/src/main/res/layout/activity_view_video.xml @@ -20,11 +20,21 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + app:horizontalPanEnabled="true" + app:verticalPanEnabled="true" + app:zoomEnabled="true" /> + +