From a283e3947360333e05def86d8838d99dcba1ae01 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 9 May 2024 15:50:30 -0400 Subject: Fix controller issues in StyledPlayerView in ExoPlayer. --- .../activities/ViewVideoActivity.java | 20 ++++ .../adapters/HistoryPostRecyclerViewAdapter.java | 133 +++++++++++++-------- .../adapters/PostDetailRecyclerViewAdapter.java | 50 +++++--- .../adapters/PostRecyclerViewAdapter.java | 133 +++++++++++++-------- .../fragments/ViewImgurVideoFragment.java | 21 ++++ .../fragments/ViewRedditGalleryVideoFragment.java | 21 ++++ .../infinityforreddit/videoautoplay/Playable.java | 7 ++ .../layout/exo_autoplay_playback_control_view.xml | 46 +++---- .../exo_autoplay_playback_control_view_legacy.xml | 7 -- .../main/res/layout/exo_playback_control_view.xml | 20 +--- 10 files changed, 281 insertions(+), 177 deletions(-) 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 c90c7cb3..54eecf43 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -38,6 +38,7 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import androidx.core.content.res.ResourcesCompat; import androidx.core.graphics.Insets; import androidx.core.view.OnApplyWindowInsetsListener; import androidx.core.view.ViewCompat; @@ -63,7 +64,9 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.SimpleCache; import com.google.android.exoplayer2.util.MimeTypes; +import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.video.VideoSize; +import com.google.android.material.button.MaterialButton; import com.google.common.collect.ImmutableList; import com.otaliastudios.zoom.ZoomEngine; import com.otaliastudios.zoom.ZoomSurfaceView; @@ -484,7 +487,24 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe }); } + MaterialButton playPauseButton = findViewById(R.id.exo_play); + Drawable playDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_play_arrow_24dp, null); + Drawable pauseDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_pause_24dp, null); + playPauseButton.setOnClickListener(view -> { + Util.handlePlayPauseButtonAction(player); + }); + player.addListener(new Player.Listener() { + @Override + public void onEvents(@NonNull Player player, @NonNull Player.Events events) { + if (events.containsAny( + Player.EVENT_PLAY_WHEN_READY_CHANGED, + Player.EVENT_PLAYBACK_STATE_CHANGED, + Player.EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED)) { + playPauseButton.setIcon(Util.shouldShowPlayButton(player) ? playDrawable : pauseDrawable); + } + } + @Override public void onTracksChanged(@NonNull Tracks tracks) { ImmutableList trackGroups = tracks.getGroups(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java index 2c7644ca..c219c5fa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java @@ -25,10 +25,12 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.content.res.AppCompatResources; import androidx.constraintlayout.widget.Barrier; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintSet; import androidx.core.content.ContextCompat; +import androidx.core.content.res.ResourcesCompat; import androidx.paging.PagingDataAdapter; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.ItemTouchHelper; @@ -44,11 +46,13 @@ import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.google.android.exoplayer2.PlaybackException; +import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Tracks; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.DefaultTimeBar; import com.google.android.exoplayer2.ui.StyledPlayerView; import com.google.android.exoplayer2.ui.TimeBar; +import com.google.android.exoplayer2.util.Util; import com.google.android.material.button.MaterialButton; import com.google.common.collect.ImmutableList; import com.libRG.CustomTextView; @@ -3114,8 +3118,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; private boolean isManuallyPaused; + private Drawable playDrawable; + private Drawable pauseDrawable; PostBaseVideoAutoplayViewHolder(View rootView, AspectRatioGifImageView iconGifImageView, @@ -3146,8 +3151,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - }); - - playButton.setOnClickListener(view -> { - isManuallyPaused = false; - play(); + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } }); progressBar.addListener(new TimeBar.OnScrubListener() { @@ -3340,6 +3345,16 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter trackGroups = tracks.getGroups(); @@ -3450,7 +3465,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; private boolean isManuallyPaused; + private Drawable playDrawable; + private Drawable pauseDrawable; PostCard2BaseVideoAutoplayViewHolder(View itemView, AspectRatioGifImageView iconGifImageView, @@ -4790,8 +4804,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - }); - - playButton.setOnClickListener(view -> { - isManuallyPaused = false; - play(); + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } }); progressBar.addListener(new TimeBar.OnScrubListener() { @@ -4986,6 +5000,16 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter trackGroups = tracks.getGroups(); @@ -5096,7 +5120,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; private boolean isManuallyPaused; + private Drawable playDrawable; + private Drawable pauseDrawable; PostMaterial3CardBaseVideoAutoplayViewHolder(View rootView, AspectRatioGifImageView iconGifImageView, @@ -5798,8 +5821,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - }); - - playButton.setOnClickListener(view -> { - isManuallyPaused = false; - play(); + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } }); progressBar.addListener(new TimeBar.OnScrubListener() { @@ -5982,6 +6005,16 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter trackGroups = tracks.getGroups(); @@ -6085,7 +6118,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { if (helper != null) { if (helper.getVolume() != 0) { - muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_mute_24dp)); + muteButton.setImageDrawable(AppCompatResources.getDrawable(mActivity, R.drawable.ic_mute_24dp)); helper.setVolume(0f); volume = 0f; } else { - muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_unmute_24dp)); + muteButton.setImageDrawable(AppCompatResources.getDrawable(mActivity, R.drawable.ic_unmute_24dp)); helper.setVolume(1f); volume = 1f; } @@ -1726,15 +1730,15 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - }); - - playButton.setOnClickListener(view -> { - isManuallyPaused = false; - play(); + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } }); progressBar.addListener(new TimeBar.OnScrubListener() { @@ -1815,6 +1819,16 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter trackGroups = tracks.getGroups(); @@ -1922,7 +1936,6 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter fetchRedgifsOrStreamableVideoCall; private boolean isManuallyPaused; + private Drawable playDrawable; + private Drawable pauseDrawable; PostBaseVideoAutoplayViewHolder(View rootView, AspectRatioGifImageView iconGifImageView, @@ -3308,8 +3313,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - }); - - playButton.setOnClickListener(view -> { - isManuallyPaused = false; - play(); + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } }); progressBar.addListener(new TimeBar.OnScrubListener() { @@ -3503,6 +3508,16 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter trackGroups = tracks.getGroups(); @@ -3613,7 +3628,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; private boolean isManuallyPaused; + private Drawable playDrawable; + private Drawable pauseDrawable; PostCard2BaseVideoAutoplayViewHolder(View itemView, AspectRatioGifImageView iconGifImageView, @@ -5020,8 +5034,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - }); - - playButton.setOnClickListener(view -> { - isManuallyPaused = false; - play(); + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } }); progressBar.addListener(new TimeBar.OnScrubListener() { @@ -5217,6 +5231,16 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter trackGroups = tracks.getGroups(); @@ -5327,7 +5351,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter fetchRedgifsOrStreamableVideoCall; private boolean isManuallyPaused; + private Drawable playDrawable; + private Drawable pauseDrawable; PostMaterial3CardBaseVideoAutoplayViewHolder(View rootView, AspectRatioGifImageView iconGifImageView, @@ -6061,8 +6084,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - pause(); - isManuallyPaused = true; - savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); - }); - - playButton.setOnClickListener(view -> { - isManuallyPaused = false; - play(); + playPauseButton.setOnClickListener(view -> { + if (isPlaying()) { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } else { + isManuallyPaused = false; + play(); + } }); progressBar.addListener(new TimeBar.OnScrubListener() { @@ -6249,6 +6272,16 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter trackGroups = tracks.getGroups(); @@ -6352,7 +6385,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + Util.handlePlayPauseButtonAction(player); + }); + player.addListener(new Player.Listener() { + @Override + public void onEvents(@NonNull Player player, @NonNull Player.Events events) { + if (events.containsAny( + Player.EVENT_PLAY_WHEN_READY_CHANGED, + Player.EVENT_PLAYBACK_STATE_CHANGED, + Player.EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED)) { + playPauseButton.setIcon(Util.shouldShowPlayButton(player) ? playDrawable : pauseDrawable); + } + } + @Override public void onTracksChanged(@NonNull Tracks tracks) { ImmutableList trackGroups = tracks.getGroups(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryVideoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryVideoFragment.java index fd7358d7..0430b848 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryVideoFragment.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; +import android.graphics.drawable.Drawable; import android.media.AudioManager; import android.os.Build; import android.os.Bundle; @@ -20,6 +21,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; +import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; import com.google.android.exoplayer2.ExoPlayer; @@ -35,6 +37,8 @@ import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.SimpleCache; +import com.google.android.exoplayer2.util.Util; +import com.google.android.material.button.MaterialButton; import com.google.common.collect.ImmutableList; import javax.inject.Inject; @@ -286,7 +290,24 @@ public class ViewRedditGalleryVideoFragment extends Fragment { binding.getMuteButton().setImageResource(R.drawable.ic_unmute_24dp); } + MaterialButton playPauseButton = binding.getRoot().findViewById(R.id.exo_play); + Drawable playDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_play_arrow_24dp, null); + Drawable pauseDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_pause_24dp, null); + playPauseButton.setOnClickListener(view -> { + Util.handlePlayPauseButtonAction(player); + }); + player.addListener(new Player.Listener() { + @Override + public void onEvents(@NonNull Player player, @NonNull Player.Events events) { + if (events.containsAny( + Player.EVENT_PLAY_WHEN_READY_CHANGED, + Player.EVENT_PLAYBACK_STATE_CHANGED, + Player.EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED)) { + playPauseButton.setIcon(Util.shouldShowPlayButton(player) ? playDrawable : pauseDrawable); + } + } + @Override public void onTracksChanged(@NonNull Tracks tracks) { ImmutableList trackGroups = tracks.getGroups(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/videoautoplay/Playable.java b/app/src/main/java/ml/docilealligator/infinityforreddit/videoautoplay/Playable.java index 4c879e57..035a3b09 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/videoautoplay/Playable.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/videoautoplay/Playable.java @@ -306,6 +306,13 @@ public interface Playable { EventListeners() { } + @Override + public void onEvents(@NonNull Player player, @NonNull Player.Events events) { + for (EventListener eventListener : this) { + eventListener.onEvents(player, events); + } + } + @Override public void onVideoSizeChanged(@NonNull VideoSize videoSize) { for (EventListener eventListener : this) { diff --git a/app/src/main/res/layout/exo_autoplay_playback_control_view.xml b/app/src/main/res/layout/exo_autoplay_playback_control_view.xml index 14c2b017..b03d5f2b 100644 --- a/app/src/main/res/layout/exo_autoplay_playback_control_view.xml +++ b/app/src/main/res/layout/exo_autoplay_playback_control_view.xml @@ -23,53 +23,37 @@ app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintEnd_toStartOf="@id/frame_layout" /> + app:layout_constraintEnd_toStartOf="@id/exo_play" /> - - - - - - - + app:layout_constraintStart_toEndOf="@id/exo_play" + app:layout_constraintEnd_toEndOf="parent" /> - - - -