From abd57e23a60b5c9e3afbd706f3ebfef9ff523414 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 9 Oct 2021 21:22:18 +0800 Subject: Change playback speed. --- .../activities/ViewVideoActivity.java | 31 ++++- .../PlaybackSpeedBottomSheetFragment.java | 126 +++++++++++++++++++++ 2 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java (limited to 'app/src/main/java/ml') 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 3f17763b..53aa6188 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -36,6 +36,7 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; 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; @@ -55,8 +56,6 @@ import com.google.android.exoplayer2.upstream.cache.SimpleCache; import com.google.android.exoplayer2.util.Util; import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.snackbar.Snackbar; -import app.futured.hauler.DragDirection; -import app.futured.hauler.HaulerView; import org.apache.commons.io.FilenameUtils; @@ -66,12 +65,15 @@ import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; +import app.futured.hauler.DragDirection; +import app.futured.hauler.HaulerView; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.apis.VReddIt; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.PlaybackSpeedBottomSheetFragment; import ml.docilealligator.infinityforreddit.font.ContentFontFamily; import ml.docilealligator.infinityforreddit.font.ContentFontStyle; import ml.docilealligator.infinityforreddit.font.FontFamily; @@ -91,6 +93,14 @@ import retrofit2.Retrofit; public class ViewVideoActivity extends AppCompatActivity { + public static final int PLAYBACK_SPEED_25 = 25; + public static final int PLAYBACK_SPEED_50 = 50; + public static final int PLAYBACK_SPEED_75 = 75; + public static final int PLAYBACK_SPEED_NORMAL = 100; + public static final int PLAYBACK_SPEED_125 = 125; + public static final int PLAYBACK_SPEED_150 = 150; + public static final int PLAYBACK_SPEED_175 = 175; + public static final int PLAYBACK_SPEED_200 = 200; public static final String EXTRA_VIDEO_DOWNLOAD_URL = "EVDU"; public static final String EXTRA_SUBREDDIT = "ES"; public static final String EXTRA_ID = "EI"; @@ -113,6 +123,7 @@ public class ViewVideoActivity extends AppCompatActivity { private static final String VIDEO_TYPE_STATE = "VTS"; private static final String SUBREDDIT_NAME_STATE = "SNS"; private static final String ID_STATE= "IS"; + private static final String PLAYBACK_SPEED_STATE = "PSS"; @BindView(R.id.hauler_view_view_video_activity) HaulerView haulerView; @@ -152,6 +163,7 @@ public class ViewVideoActivity extends AppCompatActivity { private boolean isDataSavingMode; private boolean isHd; private Integer originalOrientation; + private int playbackSpeed = 100; @Inject @Named("no_oauth") @@ -347,7 +359,9 @@ public class ViewVideoActivity extends AppCompatActivity { videoType = savedInstanceState.getInt(VIDEO_TYPE_STATE); subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); id = savedInstanceState.getString(ID_STATE); + playbackSpeed = savedInstanceState.getInt(PLAYBACK_SPEED_STATE); } + setPlaybackSpeed(playbackSpeed); if (videoType == VIDEO_TYPE_V_REDD_IT) { loadVReddItVideo(savedInstanceState); @@ -626,11 +640,23 @@ public class ViewVideoActivity extends AppCompatActivity { isDownloading = true; requestPermissionAndDownload(); return true; + } else if (itemId == R.id.action_playback_speed_view_video_activity) { + PlaybackSpeedBottomSheetFragment playbackSpeedBottomSheetFragment = new PlaybackSpeedBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PlaybackSpeedBottomSheetFragment.EXTRA_PLAYBACK_SPEED, playbackSpeed); + playbackSpeedBottomSheetFragment.setArguments(bundle); + playbackSpeedBottomSheetFragment.show(getSupportFragmentManager(), 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(this, @@ -712,5 +738,6 @@ public class ViewVideoActivity extends AppCompatActivity { outState.putString(SUBREDDIT_NAME_STATE, subredditName); outState.putString(ID_STATE, id); } + outState.putInt(PLAYBACK_SPEED_STATE, playbackSpeed); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java new file mode 100644 index 00000000..47d50a4e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PlaybackSpeedBottomSheetFragment.java @@ -0,0 +1,126 @@ +package ml.docilealligator.infinityforreddit.bottomsheetfragments; + +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.activities.ViewVideoActivity; +import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment; + +public class PlaybackSpeedBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment { + + public static final String EXTRA_PLAYBACK_SPEED = "EPS"; + + @BindView(R.id.playback_speed_025_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeed025TextView; + @BindView(R.id.playback_speed_050_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeed050TextView; + @BindView(R.id.playback_speed_075_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeed075TextView; + @BindView(R.id.playback_speed_normal_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeedNormalTextView; + @BindView(R.id.playback_speed_125_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeed125TextView; + @BindView(R.id.playback_speed_150_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeed150TextView; + @BindView(R.id.playback_speed_175_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeed175TextView; + @BindView(R.id.playback_speed_200_text_view_playback_speed_bottom_sheet_fragment) + TextView playbackSpeed200TextView; + private ViewVideoActivity viewVideoActivity; + + public PlaybackSpeedBottomSheetFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_playback_speed, container, false); + + ButterKnife.bind(this, rootView); + int playbackSpeed = getArguments().getInt(EXTRA_PLAYBACK_SPEED, ViewVideoActivity.PLAYBACK_SPEED_NORMAL); + switch (playbackSpeed) { + case ViewVideoActivity.PLAYBACK_SPEED_25: + playbackSpeed025TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + case ViewVideoActivity.PLAYBACK_SPEED_50: + playbackSpeed050TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + case ViewVideoActivity.PLAYBACK_SPEED_75: + playbackSpeed075TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + case ViewVideoActivity.PLAYBACK_SPEED_NORMAL: + playbackSpeedNormalTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + case ViewVideoActivity.PLAYBACK_SPEED_125: + playbackSpeed125TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + case ViewVideoActivity.PLAYBACK_SPEED_150: + playbackSpeed150TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + case ViewVideoActivity.PLAYBACK_SPEED_175: + playbackSpeed175TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + case ViewVideoActivity.PLAYBACK_SPEED_200: + playbackSpeed200TextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_playback_speed_24dp, 0); + break; + } + + playbackSpeed025TextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_25); + dismiss(); + }); + + playbackSpeed050TextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_50); + dismiss(); + }); + + playbackSpeed075TextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_75); + dismiss(); + }); + + playbackSpeedNormalTextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_NORMAL); + dismiss(); + }); + + playbackSpeed125TextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_125); + dismiss(); + }); + + playbackSpeed150TextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_150); + dismiss(); + }); + + playbackSpeed175TextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_175); + dismiss(); + }); + + playbackSpeed200TextView.setOnClickListener(view -> { + viewVideoActivity.setPlaybackSpeed(ViewVideoActivity.PLAYBACK_SPEED_200); + dismiss(); + }); + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + viewVideoActivity = (ViewVideoActivity) context; + } +} \ No newline at end of file -- cgit v1.2.3