From c14c223e2f8110d47e0e5a71fa73f97bd0d7f764 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 30 Aug 2019 01:56:52 +0200 Subject: Allow different playback speed for video --- .../antennapod/activity/AudioplayerActivity.java | 11 +----- .../antennapod/activity/MediaplayerActivity.java | 23 ++++++----- .../antennapod/dialog/PlaybackControlsDialog.java | 46 ++++++++++++++++------ .../danoeh/antennapod/fragment/QueueFragment.java | 2 +- 4 files changed, 50 insertions(+), 32 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java index 2bcd7a461..9db860598 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -76,14 +76,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { } float speed = 1.0f; if(controller.canSetPlaybackSpeed()) { - try { - // we can only retrieve the playback speed from the controller/playback service - // once mediaplayer has been initialized - speed = Float.parseFloat(UserPreferences.getPlaybackSpeed()); - } catch (NumberFormatException e) { - Log.e(TAG, Log.getStackTraceString(e)); - UserPreferences.setPlaybackSpeed(String.valueOf(speed)); - } + speed = UserPreferences.getPlaybackSpeed(); } String speedStr = new DecimalFormat("0.00x").format(speed); butPlaybackSpeed.setText(speedStr); @@ -105,7 +98,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { } if (controller.canSetPlaybackSpeed()) { String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); - String currentSpeed = UserPreferences.getPlaybackSpeed(); + String currentSpeed = new DecimalFormat("0.00x").format(UserPreferences.getPlaybackSpeed()); // Provide initial value in case the speed list has changed // out from under us diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index 4c64c46da..52497a27f 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -357,7 +357,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements menu.findItem(R.id.audio_controls).setIcon(new IconDrawable(this, FontAwesomeIcons.fa_sliders).color(textColor).actionBarSize()); } else { - menu.findItem(R.id.audio_controls).setVisible(false); + menu.findItem(R.id.audio_controls).setIcon(new IconDrawable(this, + FontAwesomeIcons.fa_sliders).color(0xffffffff).actionBarSize()); } return true; @@ -443,8 +444,9 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements } break; case R.id.audio_controls: - PlaybackControlsDialog playbackControlsDialog = new PlaybackControlsDialog(); - playbackControlsDialog.show(getSupportFragmentManager(), "playback_controls"); + boolean isPlayingVideo = controller.getMedia().getMediaType() == MediaType.VIDEO; + PlaybackControlsDialog dialog = PlaybackControlsDialog.newInstance(isPlayingVideo); + dialog.show(getSupportFragmentManager(), "playback_controls"); break; case R.id.visit_website_item: Uri uri = Uri.parse(getWebsiteLinkWithFallback(media)); @@ -518,9 +520,10 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements return; } - int currentPosition = TimeSpeedConverter.convert(controller.getPosition()); - int duration = TimeSpeedConverter.convert(controller.getDuration()); - int remainingTime = TimeSpeedConverter.convert( + TimeSpeedConverter converter = new TimeSpeedConverter(controller.getCurrentPlaybackSpeedMultiplier()); + int currentPosition = converter.convert(controller.getPosition()); + int duration = converter.convert(controller.getDuration()); + int remainingTime = converter.convert( controller.getDuration() - controller.getPosition()); Log.d(TAG, "currentPosition " + Converter.getDurationStringLong(currentPosition)); if (currentPosition == PlaybackService.INVALID_TIME || @@ -676,14 +679,15 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements return; } + TimeSpeedConverter converter = new TimeSpeedConverter(controller.getCurrentPlaybackSpeedMultiplier()); String length; if (showTimeLeft) { - int remainingTime = TimeSpeedConverter.convert( + int remainingTime = converter.convert( media.getDuration() - media.getPosition()); length = "-" + Converter.getDurationStringLong(remainingTime); } else { - int duration = TimeSpeedConverter.convert(media.getDuration()); + int duration = converter.convert(media.getDuration()); length = Converter.getDurationStringLong(duration); } txtvLength.setText(length); @@ -787,7 +791,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition); if (showTimeLeft && prog != 0) { int duration = controller.getDuration(); - int timeLeft = TimeSpeedConverter.convert(duration - (int) (prog * duration)); + TimeSpeedConverter converter = new TimeSpeedConverter(controller.getCurrentPlaybackSpeedMultiplier()); + int timeLeft = converter.convert(duration - (int) (prog * duration)); String length = "-" + Converter.getDurationStringLong(timeLeft); txtvLength.setText(length); } diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java index 047aaca19..e8c7520b7 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java @@ -4,7 +4,6 @@ import android.app.Dialog; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; -import android.util.Log; import android.widget.Button; import android.widget.CheckBox; import android.widget.SeekBar; @@ -21,9 +20,19 @@ public class PlaybackControlsDialog extends DialogFragment { private static final float PLAYBACK_SPEED_STEP = 0.05f; private static final float DEFAULT_MIN_PLAYBACK_SPEED = 0.5f; private static final float DEFAULT_MAX_PLAYBACK_SPEED = 2.5f; - private static final String TAG = "AudioControlsDialog"; + private static final String ARGUMENT_IS_PLAYING_VIDEO = "isPlayingVideo"; private PlaybackController controller; + private MaterialDialog dialog; + private boolean isPlayingVideo; + + public static PlaybackControlsDialog newInstance(boolean isPlayingVideo) { + Bundle arguments = new Bundle(); + arguments.putBoolean(ARGUMENT_IS_PLAYING_VIDEO, isPlayingVideo); + PlaybackControlsDialog dialog = new PlaybackControlsDialog(); + dialog.setArguments(arguments); + return dialog; + } public PlaybackControlsDialog() { // Empty constructor required for DialogFragment @@ -34,6 +43,7 @@ public class PlaybackControlsDialog extends DialogFragment { super.onStart(); controller = new PlaybackController(getActivity(), false); controller.init(); + setupUi(); } @Override @@ -46,7 +56,9 @@ public class PlaybackControlsDialog extends DialogFragment { @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - MaterialDialog dialog = new MaterialDialog.Builder(getContext()) + isPlayingVideo = getArguments() != null && getArguments().getBoolean(ARGUMENT_IS_PLAYING_VIDEO); + + dialog = new MaterialDialog.Builder(getContext()) .title(R.string.audio_controls) .customView(R.layout.audio_controls, true) .neutralText(R.string.close_label) @@ -55,7 +67,10 @@ public class PlaybackControlsDialog extends DialogFragment { final SeekBar right = (SeekBar) dialog1.findViewById(R.id.volume_right); UserPreferences.setVolume(left.getProgress(), right.getProgress()); }).build(); + return dialog; + } + private void setupUi() { final SeekBar barPlaybackSpeed = (SeekBar) dialog.findViewById(R.id.playback_speed); final Button butDecSpeed = (Button) dialog.findViewById(R.id.butDecSpeed); butDecSpeed.setOnClickListener(v -> { @@ -75,13 +90,7 @@ public class PlaybackControlsDialog extends DialogFragment { }); final TextView txtvPlaybackSpeed = (TextView) dialog.findViewById(R.id.txtvPlaybackSpeed); - float currentSpeed = 1.0f; - try { - currentSpeed = Float.parseFloat(UserPreferences.getPlaybackSpeed()); - } catch (NumberFormatException e) { - Log.e(TAG, Log.getStackTraceString(e)); - UserPreferences.setPlaybackSpeed(String.valueOf(currentSpeed)); - } + float currentSpeed = getCurrentSpeed(); String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); final float minPlaybackSpeed = availableSpeeds.length > 1 ? @@ -99,11 +108,17 @@ public class PlaybackControlsDialog extends DialogFragment { float playbackSpeed = progress * PLAYBACK_SPEED_STEP + minPlaybackSpeed; controller.setPlaybackSpeed(playbackSpeed); String speedPref = String.format(Locale.US, "%.2f", playbackSpeed); - UserPreferences.setPlaybackSpeed(speedPref); + + if (isPlayingVideo) { + UserPreferences.setVideoPlaybackSpeed(speedPref); + } else { + UserPreferences.setPlaybackSpeed(speedPref); + } + String speedStr = String.format("%.2fx", playbackSpeed); txtvPlaybackSpeed.setText(speedStr); } else if (fromUser) { - float speed = Float.valueOf(UserPreferences.getPlaybackSpeed()); + float speed = getCurrentSpeed(); barPlaybackSpeed.post(() -> barPlaybackSpeed.setProgress( (int) ((speed - minPlaybackSpeed) / PLAYBACK_SPEED_STEP))); } @@ -188,7 +203,12 @@ public class PlaybackControlsDialog extends DialogFragment { controller.setDownmix(isChecked); } }); + } - return dialog; + private float getCurrentSpeed() { + if (isPlayingVideo) { + return UserPreferences.getVideoPlaybackSpeed(); + } + return UserPreferences.getPlaybackSpeed(); } } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index 0fe413954..aaff12c10 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -596,7 +596,7 @@ public class QueueFragment extends Fragment { String info = queue.size() + getString(R.string.episodes_suffix); if(queue.size() > 0) { long timeLeft = 0; - float playbackSpeed = Float.valueOf(UserPreferences.getPlaybackSpeed()); + float playbackSpeed = UserPreferences.getPlaybackSpeed(); for(FeedItem item : queue) { if(item.getMedia() != null) { timeLeft += -- cgit v1.2.3