Set playback speed in other ViewImgurVideoFragment and ViewRedditGalleryVideoFragment.

This commit is contained in:
Alex Ning 2021-10-10 20:20:14 +08:00
parent 4de457cd02
commit a30421e73b
8 changed files with 99 additions and 24 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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

View File

@ -1,19 +1,19 @@
<vector android:height="15.565753dp" android:viewportHeight="115.9"
<vector android:height="24dp" android:viewportHeight="178.7"
android:viewportWidth="178.7" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFB026" android:pathData="M59.106,104.943C58.766,108.82 53.687,111.541 47.763,111.021C41.838,110.502 37.311,106.938 37.651,103.061C37.991,99.184 43.07,96.463 48.995,96.983C54.919,97.503 59.447,101.067 59.106,104.943Z"/>
<path android:fillColor="#FFB026" android:pathData="M102.258,107.79C102.258,112.269 96.306,115.9 88.963,115.9C81.62,115.9 75.668,112.269 75.668,107.79C75.668,103.311 81.62,99.68 88.963,99.68C96.306,99.68 102.258,103.311 102.258,107.79Z"/>
<path android:fillColor="#FFB026" android:pathData="M141.137,102.653C141.689,105.687 137.501,110.287 131.433,111.39C125.365,112.493 119.713,109.682 119.161,106.648C118.61,103.614 123.082,100.26 129.151,99.156C135.219,98.053 140.585,99.618 141.137,102.653Z"/>
<path android:fillColor="#5F63C0" android:pathData="M178.715,92.644C178.715,92.644 169.54,72.905 106.315,63.972L89.358,64.393L72.401,63.972C9.174,72.905 0,92.644 0,92.644C0,92.644 38.462,106.197 89.358,105.884C140.253,106.197 178.715,92.644 178.715,92.644Z"/>
<path android:fillColor="#FB4A4C" android:pathData="M160.126,87.258C160.586,88.959 158.603,90.975 155.696,91.761C152.79,92.546 150.062,91.804 149.602,90.103C149.142,88.402 151.125,86.386 154.031,85.6C156.938,84.815 159.666,85.557 160.126,87.258Z"/>
<!--<path android:fillColor="#FB4A4C" android:pathData="M160.126,87.258C160.586,88.959 158.603,90.975 155.696,91.761C152.79,92.546 150.062,91.804 149.602,90.103C149.142,88.402 151.125,86.386 154.031,85.6C156.938,84.815 159.666,85.557 160.126,87.258Z"/>
<path android:fillColor="#FB4A4C" android:pathData="M129.735,93.072C129.906,94.826 127.615,96.484 124.619,96.776C121.623,97.067 119.055,95.882 118.884,94.128C118.714,92.374 121.005,90.716 124.001,90.424C126.997,90.133 129.564,91.318 129.735,93.072Z"/>
<path android:fillColor="#FB4A4C" android:pathData="M94.136,95.857C94.148,97.619 91.717,99.064 88.706,99.084C85.696,99.105 83.245,97.692 83.234,95.93C83.222,94.168 85.653,92.723 88.663,92.703C91.674,92.683 94.124,94.095 94.136,95.857Z"/>
<path android:fillColor="#FB4A4C" android:pathData="M59.464,93.73C59.145,95.463 56.485,96.425 53.525,95.878C50.565,95.332 48.424,93.484 48.743,91.751C49.064,90.018 51.723,89.057 54.683,89.603C57.644,90.149 59.784,91.997 59.464,93.73Z"/>
<path android:fillColor="#FB4A4C" android:pathData="M28.996,86.395C28.766,88.142 26.159,89.24 23.175,88.846C20.189,88.453 17.957,86.717 18.187,84.97C18.418,83.223 21.023,82.126 24.009,82.52C26.993,82.913 29.225,84.648 28.996,86.395Z"/>
<path android:fillColor="#FB4A4C" android:pathData="M28.996,86.395C28.766,88.142 26.159,89.24 23.175,88.846C20.189,88.453 17.957,86.717 18.187,84.97C18.418,83.223 21.023,82.126 24.009,82.52C26.993,82.913 29.225,84.648 28.996,86.395Z"/>-->
<path android:fillColor="#FFF0BB" android:pathData="M87.892,89.208C71.541,89.208 52.576,86.016 32.232,76.578L31.926,76.437L31.899,76.102C31.885,75.944 30.662,60.104 36.924,42.768C41.531,30.02 51.04,14.357 70.658,4.823C86.562,-2.907 105.645,-1.398 119.39,9.726C119.874,10.119 120.358,10.525 120.839,10.945C135.506,23.754 143.671,45.214 145.111,74.727L145.127,75.066L144.84,75.245C144.506,75.453 121.833,89.208 87.892,89.208Z"/>
<path android:fillColor="#00B57C" android:pathData="M135.72,63.151C133.991,58.451 131.692,53.135 128.687,47.958C130.489,47.266 131.774,45.531 131.774,43.485C131.774,40.833 129.623,38.682 126.97,38.682C125.592,38.682 124.358,39.271 123.482,40.201C115.263,29.671 103.321,21.651 86.15,23.975C71.072,26.016 60.61,32.117 53.363,39.96C52.507,39.174 51.376,38.682 50.122,38.682C47.47,38.682 45.32,40.833 45.32,43.485C45.32,45.208 46.234,46.706 47.595,47.554C41.177,57.863 38.687,69.633 37.831,79.02C56.055,86.562 73.052,89.208 87.892,89.208C113.061,89.208 132.023,81.648 140.284,77.653C139.118,73.269 137.626,68.338 135.72,63.151Z"/>
<path android:fillColor="#032560" android:pathData="M83.377,60.695C84.303,62.182 85.709,63.554 87.344,64.215C90.587,65.525 93.246,63.307 95.119,60.841C95.83,59.906 94.246,58.996 93.544,59.92C91.895,62.091 89.61,63.759 87.055,61.991C86.231,61.421 85.484,60.628 84.953,59.774C84.334,58.779 82.755,59.694 83.377,60.695Z"/>
<path android:fillColor="#032560" android:pathData="M77.686,50.012C77.686,51.161 76.754,52.093 75.606,52.093C74.457,52.093 73.525,51.161 73.525,50.012C73.525,48.863 74.457,47.931 75.606,47.931C76.754,47.931 77.686,48.863 77.686,50.012Z"/>
<path android:fillColor="#032560" android:pathData="M104.372,50.012C104.372,51.161 103.44,52.093 102.291,52.093C101.143,52.093 100.211,51.161 100.211,50.012C100.211,48.863 101.143,47.931 102.291,47.931C103.44,47.931 104.372,48.863 104.372,50.012Z"/>
<path android:fillColor="#FFAB3D" android:pathData="M76.298,58.828C76.298,60.866 74.422,62.518 72.108,62.518C69.794,62.518 67.918,60.866 67.918,58.828C67.918,56.79 69.794,55.139 72.108,55.139C74.422,55.139 76.298,56.79 76.298,58.828Z"/>
<path android:fillColor="#FFAB3D" android:pathData="M109.016,58.828C109.016,60.866 107.14,62.518 104.826,62.518C102.511,62.518 100.636,60.866 100.636,58.828C100.636,56.79 102.511,55.139 104.826,55.139C107.14,55.139 109.016,56.79 109.016,58.828Z"/>
<!--<path android:fillColor="#00B57C" android:pathData="M135.72,63.151C133.991,58.451 131.692,53.135 128.687,47.958C130.489,47.266 131.774,45.531 131.774,43.485C131.774,40.833 129.623,38.682 126.97,38.682C125.592,38.682 124.358,39.271 123.482,40.201C115.263,29.671 103.321,21.651 86.15,23.975C71.072,26.016 60.61,32.117 53.363,39.96C52.507,39.174 51.376,38.682 50.122,38.682C47.47,38.682 45.32,40.833 45.32,43.485C45.32,45.208 46.234,46.706 47.595,47.554C41.177,57.863 38.687,69.633 37.831,79.02C56.055,86.562 73.052,89.208 87.892,89.208C113.061,89.208 132.023,81.648 140.284,77.653C139.118,73.269 137.626,68.338 135.72,63.151Z"/>-->
<!--<path android:fillColor="#032560" android:pathData="M83.377,60.695C84.303,62.182 85.709,63.554 87.344,64.215C90.587,65.525 93.246,63.307 95.119,60.841C95.83,59.906 94.246,58.996 93.544,59.92C91.895,62.091 89.61,63.759 87.055,61.991C86.231,61.421 85.484,60.628 84.953,59.774C84.334,58.779 82.755,59.694 83.377,60.695Z"/>-->
<!--<path android:fillColor="#032560" android:pathData="M77.686,50.012C77.686,51.161 76.754,52.093 75.606,52.093C74.457,52.093 73.525,51.161 73.525,50.012C73.525,48.863 74.457,47.931 75.606,47.931C76.754,47.931 77.686,48.863 77.686,50.012Z"/>-->
<!--<path android:fillColor="#032560" android:pathData="M104.372,50.012C104.372,51.161 103.44,52.093 102.291,52.093C101.143,52.093 100.211,51.161 100.211,50.012C100.211,48.863 101.143,47.931 102.291,47.931C103.44,47.931 104.372,48.863 104.372,50.012Z"/>-->
<!--<path android:fillColor="#FFAB3D" android:pathData="M76.298,58.828C76.298,60.866 74.422,62.518 72.108,62.518C69.794,62.518 67.918,60.866 67.918,58.828C67.918,56.79 69.794,55.139 72.108,55.139C74.422,55.139 76.298,56.79 76.298,58.828Z"/>-->
<!--<path android:fillColor="#FFAB3D" android:pathData="M109.016,58.828C109.016,60.866 107.14,62.518 104.826,62.518C102.511,62.518 100.636,60.866 100.636,58.828C100.636,56.79 102.511,55.139 104.826,55.139C107.14,55.139 109.016,56.79 109.016,58.828Z"/>-->
</vector>

View File

@ -7,4 +7,11 @@
android:title="@string/action_download"
android:icon="@drawable/ic_file_download_toolbar_white_24dp"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_playback_speed_view_imgur_video_fragment"
android:orderInCategory="2"
android:title="@string/action_playback_speed"
android:icon="@drawable/ic_playback_speed_toolbar_24dp"
app:showAsAction="ifRoom" />
</menu>

View File

@ -7,4 +7,11 @@
android:title="@string/action_download"
android:icon="@drawable/ic_file_download_toolbar_white_24dp"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_playback_speed_view_reddit_gallery_video_fragment"
android:orderInCategory="2"
android:title="@string/action_playback_speed"
android:icon="@drawable/ic_playback_speed_toolbar_24dp"
app:showAsAction="ifRoom" />
</menu>