diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-10-10 12:20:14 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-10-10 12:20:14 +0000 |
commit | a30421e73b645bdbab97735875c26a77f27c0bac (patch) | |
tree | 55dd606ae5f42de9cdae3ff0e6b9b942c3892ab5 /app/src/main/java/ml | |
parent | 4de457cd0207dc1fcac1487d046565df06c3d165 (diff) | |
download | infinity-for-reddit-a30421e73b645bdbab97735875c26a77f27c0bac.tar infinity-for-reddit-a30421e73b645bdbab97735875c26a77f27c0bac.tar.gz infinity-for-reddit-a30421e73b645bdbab97735875c26a77f27c0bac.tar.bz2 infinity-for-reddit-a30421e73b645bdbab97735875c26a77f27c0bac.tar.lz infinity-for-reddit-a30421e73b645bdbab97735875c26a77f27c0bac.tar.xz infinity-for-reddit-a30421e73b645bdbab97735875c26a77f27c0bac.tar.zst infinity-for-reddit-a30421e73b645bdbab97735875c26a77f27c0bac.zip |
Set playback speed in other ViewImgurVideoFragment and ViewRedditGalleryVideoFragment.
Diffstat (limited to 'app/src/main/java/ml')
4 files changed, 76 insertions, 15 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 b09b66a2..3f61ed7a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -613,7 +613,7 @@ public class ViewVideoActivity extends AppCompatActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.view_video, menu); + getMenuInflater().inflate(R.menu.view_video_activity, menu); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java index 47d50a4e..8957b56b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit.bottomsheetfragments; +import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; @@ -8,12 +9,15 @@ import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.activities.ViewVideoActivity; import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment; +import ml.docilealligator.infinityforreddit.fragments.ViewImgurVideoFragment; +import ml.docilealligator.infinityforreddit.fragments.ViewRedditGalleryVideoFragment; public class PlaybackSpeedBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment { @@ -35,7 +39,7 @@ public class PlaybackSpeedBottomSheetFragment extends LandscapeExpandedRoundedBo TextView playbackSpeed175TextView; @BindView(R.id.playback_speed_200_text_view_playback_speed_bottom_sheet_fragment) TextView playbackSpeed200TextView; - private ViewVideoActivity viewVideoActivity; + private Activity activity; public PlaybackSpeedBottomSheetFragment() { // Required empty public constructor @@ -77,50 +81,63 @@ public class PlaybackSpeedBottomSheetFragment extends LandscapeExpandedRoundedBo } playbackSpeed025TextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_25); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_25); dismiss(); }); playbackSpeed050TextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_50); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_50); dismiss(); }); playbackSpeed075TextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_75); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_75); dismiss(); }); playbackSpeedNormalTextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_NORMAL); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_NORMAL); dismiss(); }); playbackSpeed125TextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_125); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_125); dismiss(); }); playbackSpeed150TextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_150); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_150); dismiss(); }); playbackSpeed175TextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_175); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_175); dismiss(); }); playbackSpeed200TextView.setOnClickListener(view -> { - viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_200); + setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_200); dismiss(); }); return rootView; } + private void setPlaybackSpeed(int playbackSpeed) { + if (activity instanceof ViewVideoActivity) { + ((ViewVideoActivity) activity).setPlaybackSpeed(playbackSpeed); + } else { + Fragment parentFragment = getParentFragment(); + if (parentFragment instanceof ViewImgurVideoFragment) { + ((ViewImgurVideoFragment) parentFragment).setPlaybackSpeed(playbackSpeed); + } else if (parentFragment instanceof ViewRedditGalleryVideoFragment) { + ((ViewRedditGalleryVideoFragment) parentFragment).setPlaybackSpeed(playbackSpeed); + } + } + } + @Override public void onAttach(@NonNull Context context) { super.onAttach(context); - viewVideoActivity = (ViewVideoActivity) context; + activity = (Activity) context; } }
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurVideoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurVideoFragment.java index 99a950e3..fcce3d4d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurVideoFragment.java @@ -27,6 +27,7 @@ import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import com.google.android.exoplayer2.ExoPlayerFactory; +import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.ProgressiveMediaSource; @@ -51,17 +52,19 @@ import ml.docilealligator.infinityforreddit.ImgurMedia; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.activities.ViewImgurMediaActivity; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.PlaybackSpeedBottomSheetFragment; import ml.docilealligator.infinityforreddit.services.DownloadMediaService; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class ViewImgurVideoFragment extends Fragment { public static final String EXTRA_IMGUR_VIDEO = "EIV"; - private static final String IS_MUTE_STATE = "IMS"; - private static final String POSITION_STATE = "PS"; public static final String EXTRA_INDEX = "EI"; public static final String EXTRA_MEDIA_COUNT = "EMC"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; + private static final String IS_MUTE_STATE = "IMS"; + private static final String POSITION_STATE = "PS"; + private static final String PLAYBACK_SPEED_STATE = "PSS"; @BindView(R.id.player_view_view_imgur_video_fragment) PlayerView videoPlayerView; @BindView(R.id.mute_exo_playback_control_view) @@ -79,6 +82,7 @@ public class ViewImgurVideoFragment extends Fragment { private boolean wasPlaying = false; private boolean isMute = false; private boolean isDownloading = false; + private int playbackSpeed = 100; @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -145,6 +149,11 @@ public class ViewImgurVideoFragment extends Fragment { dataSourceFactory = new DefaultDataSourceFactory(activity, Util.getUserAgent(activity, "Infinity")); player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(imgurMedia.getLink()))); + + if (savedInstanceState != null) { + playbackSpeed = savedInstanceState.getInt(PLAYBACK_SPEED_STATE); + } + setPlaybackSpeed(playbackSpeed); preparePlayer(savedInstanceState); if (activity.isUseBottomAppBar()) { @@ -175,10 +184,22 @@ public class ViewImgurVideoFragment extends Fragment { isDownloading = true; requestPermissionAndDownload(); return true; + } else if (item.getItemId() == R.id.action_playback_speed_view_imgur_video_fragment) { + PlaybackSpeedBottomSheetFragment playbackSpeedBottomSheetFragment = new PlaybackSpeedBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PlaybackSpeedBottomSheetFragment.EXTRA_PLAYBACK_SPEED, playbackSpeed); + playbackSpeedBottomSheetFragment.setArguments(bundle); + playbackSpeedBottomSheetFragment.show(getChildFragmentManager(), playbackSpeedBottomSheetFragment.getTag()); + return true; } return false; } + public void setPlaybackSpeed(int speed100X) { + this.playbackSpeed = speed100X; + player.setPlaybackParameters(new PlaybackParameters((float) (speed100X / 100.0))); + } + private void requestPermissionAndDownload() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (ContextCompat.checkSelfPermission(activity, @@ -301,6 +322,7 @@ public class ViewImgurVideoFragment extends Fragment { super.onSaveInstanceState(outState); outState.putBoolean(IS_MUTE_STATE, isMute); outState.putLong(POSITION_STATE, player.getCurrentPosition()); + outState.putInt(PLAYBACK_SPEED_STATE, playbackSpeed); } @Override 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 7b1867c8..ff17bd82 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryVideoFragment.java @@ -27,6 +27,7 @@ import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import com.google.android.exoplayer2.ExoPlayerFactory; +import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.ProgressiveMediaSource; @@ -50,6 +51,7 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.activities.ViewRedditGalleryActivity; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.PlaybackSpeedBottomSheetFragment; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.services.DownloadMediaService; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -58,11 +60,12 @@ public class ViewRedditGalleryVideoFragment extends Fragment { public static final String EXTRA_REDDIT_GALLERY_VIDEO = "EIV"; public static final String EXTRA_SUBREDDIT_NAME = "ESN"; - private static final String IS_MUTE_STATE = "IMS"; - private static final String POSITION_STATE = "PS"; public static final String EXTRA_INDEX = "EI"; public static final String EXTRA_MEDIA_COUNT = "EMC"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; + private static final String IS_MUTE_STATE = "IMS"; + private static final String POSITION_STATE = "PS"; + private static final String PLAYBACK_SPEED_STATE = "PSS"; @BindView(R.id.player_view_view_reddit_gallery_video_fragment) PlayerView videoPlayerView; @BindView(R.id.mute_exo_playback_control_view) @@ -81,6 +84,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment { private boolean wasPlaying = false; private boolean isMute = false; private boolean isDownloading = false; + private int playbackSpeed = 100; @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -148,6 +152,11 @@ public class ViewRedditGalleryVideoFragment extends Fragment { dataSourceFactory = new DefaultDataSourceFactory(activity, Util.getUserAgent(activity, "Infinity")); player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(galleryVideo.url))); + + if (savedInstanceState != null) { + playbackSpeed = savedInstanceState.getInt(PLAYBACK_SPEED_STATE); + } + setPlaybackSpeed(playbackSpeed); preparePlayer(savedInstanceState); if (activity.isUseBottomAppBar()) { @@ -178,10 +187,22 @@ public class ViewRedditGalleryVideoFragment extends Fragment { isDownloading = true; requestPermissionAndDownload(); return true; + } else if (item.getItemId() == R.id.action_playback_speed_view_reddit_gallery_video_fragment) { + PlaybackSpeedBottomSheetFragment playbackSpeedBottomSheetFragment = new PlaybackSpeedBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PlaybackSpeedBottomSheetFragment.EXTRA_PLAYBACK_SPEED, playbackSpeed); + playbackSpeedBottomSheetFragment.setArguments(bundle); + playbackSpeedBottomSheetFragment.show(getChildFragmentManager(), playbackSpeedBottomSheetFragment.getTag()); + return true; } return false; } + public void setPlaybackSpeed(int speed100X) { + this.playbackSpeed = speed100X; + player.setPlaybackParameters(new PlaybackParameters((float) (speed100X / 100.0))); + } + private void requestPermissionAndDownload() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { if (ContextCompat.checkSelfPermission(activity, @@ -305,6 +326,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment { super.onSaveInstanceState(outState); outState.putBoolean(IS_MUTE_STATE, isMute); outState.putLong(POSITION_STATE, player.getCurrentPosition()); + outState.putInt(PLAYBACK_SPEED_STATE, playbackSpeed); } @Override |