Allow different playback speed for video

This commit is contained in:
ByteHamster 2019-08-30 01:56:52 +02:00
parent a445942cec
commit c14c223e2f
9 changed files with 97 additions and 63 deletions

View File

@ -76,14 +76,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity {
} }
float speed = 1.0f; float speed = 1.0f;
if(controller.canSetPlaybackSpeed()) { if(controller.canSetPlaybackSpeed()) {
try { speed = UserPreferences.getPlaybackSpeed();
// 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));
}
} }
String speedStr = new DecimalFormat("0.00x").format(speed); String speedStr = new DecimalFormat("0.00x").format(speed);
butPlaybackSpeed.setText(speedStr); butPlaybackSpeed.setText(speedStr);
@ -105,7 +98,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity {
} }
if (controller.canSetPlaybackSpeed()) { if (controller.canSetPlaybackSpeed()) {
String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); 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 // Provide initial value in case the speed list has changed
// out from under us // out from under us

View File

@ -357,7 +357,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
menu.findItem(R.id.audio_controls).setIcon(new IconDrawable(this, menu.findItem(R.id.audio_controls).setIcon(new IconDrawable(this,
FontAwesomeIcons.fa_sliders).color(textColor).actionBarSize()); FontAwesomeIcons.fa_sliders).color(textColor).actionBarSize());
} else { } 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; return true;
@ -443,8 +444,9 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
} }
break; break;
case R.id.audio_controls: case R.id.audio_controls:
PlaybackControlsDialog playbackControlsDialog = new PlaybackControlsDialog(); boolean isPlayingVideo = controller.getMedia().getMediaType() == MediaType.VIDEO;
playbackControlsDialog.show(getSupportFragmentManager(), "playback_controls"); PlaybackControlsDialog dialog = PlaybackControlsDialog.newInstance(isPlayingVideo);
dialog.show(getSupportFragmentManager(), "playback_controls");
break; break;
case R.id.visit_website_item: case R.id.visit_website_item:
Uri uri = Uri.parse(getWebsiteLinkWithFallback(media)); Uri uri = Uri.parse(getWebsiteLinkWithFallback(media));
@ -518,9 +520,10 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
return; return;
} }
int currentPosition = TimeSpeedConverter.convert(controller.getPosition()); TimeSpeedConverter converter = new TimeSpeedConverter(controller.getCurrentPlaybackSpeedMultiplier());
int duration = TimeSpeedConverter.convert(controller.getDuration()); int currentPosition = converter.convert(controller.getPosition());
int remainingTime = TimeSpeedConverter.convert( int duration = converter.convert(controller.getDuration());
int remainingTime = converter.convert(
controller.getDuration() - controller.getPosition()); controller.getDuration() - controller.getPosition());
Log.d(TAG, "currentPosition " + Converter.getDurationStringLong(currentPosition)); Log.d(TAG, "currentPosition " + Converter.getDurationStringLong(currentPosition));
if (currentPosition == PlaybackService.INVALID_TIME || if (currentPosition == PlaybackService.INVALID_TIME ||
@ -676,14 +679,15 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
return; return;
} }
TimeSpeedConverter converter = new TimeSpeedConverter(controller.getCurrentPlaybackSpeedMultiplier());
String length; String length;
if (showTimeLeft) { if (showTimeLeft) {
int remainingTime = TimeSpeedConverter.convert( int remainingTime = converter.convert(
media.getDuration() - media.getPosition()); media.getDuration() - media.getPosition());
length = "-" + Converter.getDurationStringLong(remainingTime); length = "-" + Converter.getDurationStringLong(remainingTime);
} else { } else {
int duration = TimeSpeedConverter.convert(media.getDuration()); int duration = converter.convert(media.getDuration());
length = Converter.getDurationStringLong(duration); length = Converter.getDurationStringLong(duration);
} }
txtvLength.setText(length); txtvLength.setText(length);
@ -787,7 +791,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition); prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition);
if (showTimeLeft && prog != 0) { if (showTimeLeft && prog != 0) {
int duration = controller.getDuration(); 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); String length = "-" + Converter.getDurationStringLong(timeLeft);
txtvLength.setText(length); txtvLength.setText(length);
} }

View File

@ -4,7 +4,6 @@ import android.app.Dialog;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.SeekBar; 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 PLAYBACK_SPEED_STEP = 0.05f;
private static final float DEFAULT_MIN_PLAYBACK_SPEED = 0.5f; private static final float DEFAULT_MIN_PLAYBACK_SPEED = 0.5f;
private static final float DEFAULT_MAX_PLAYBACK_SPEED = 2.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 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() { public PlaybackControlsDialog() {
// Empty constructor required for DialogFragment // Empty constructor required for DialogFragment
@ -34,6 +43,7 @@ public class PlaybackControlsDialog extends DialogFragment {
super.onStart(); super.onStart();
controller = new PlaybackController(getActivity(), false); controller = new PlaybackController(getActivity(), false);
controller.init(); controller.init();
setupUi();
} }
@Override @Override
@ -46,7 +56,9 @@ public class PlaybackControlsDialog extends DialogFragment {
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { 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) .title(R.string.audio_controls)
.customView(R.layout.audio_controls, true) .customView(R.layout.audio_controls, true)
.neutralText(R.string.close_label) .neutralText(R.string.close_label)
@ -55,7 +67,10 @@ public class PlaybackControlsDialog extends DialogFragment {
final SeekBar right = (SeekBar) dialog1.findViewById(R.id.volume_right); final SeekBar right = (SeekBar) dialog1.findViewById(R.id.volume_right);
UserPreferences.setVolume(left.getProgress(), right.getProgress()); UserPreferences.setVolume(left.getProgress(), right.getProgress());
}).build(); }).build();
return dialog;
}
private void setupUi() {
final SeekBar barPlaybackSpeed = (SeekBar) dialog.findViewById(R.id.playback_speed); final SeekBar barPlaybackSpeed = (SeekBar) dialog.findViewById(R.id.playback_speed);
final Button butDecSpeed = (Button) dialog.findViewById(R.id.butDecSpeed); final Button butDecSpeed = (Button) dialog.findViewById(R.id.butDecSpeed);
butDecSpeed.setOnClickListener(v -> { butDecSpeed.setOnClickListener(v -> {
@ -75,13 +90,7 @@ public class PlaybackControlsDialog extends DialogFragment {
}); });
final TextView txtvPlaybackSpeed = (TextView) dialog.findViewById(R.id.txtvPlaybackSpeed); final TextView txtvPlaybackSpeed = (TextView) dialog.findViewById(R.id.txtvPlaybackSpeed);
float currentSpeed = 1.0f; float currentSpeed = getCurrentSpeed();
try {
currentSpeed = Float.parseFloat(UserPreferences.getPlaybackSpeed());
} catch (NumberFormatException e) {
Log.e(TAG, Log.getStackTraceString(e));
UserPreferences.setPlaybackSpeed(String.valueOf(currentSpeed));
}
String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray();
final float minPlaybackSpeed = availableSpeeds.length > 1 ? final float minPlaybackSpeed = availableSpeeds.length > 1 ?
@ -99,11 +108,17 @@ public class PlaybackControlsDialog extends DialogFragment {
float playbackSpeed = progress * PLAYBACK_SPEED_STEP + minPlaybackSpeed; float playbackSpeed = progress * PLAYBACK_SPEED_STEP + minPlaybackSpeed;
controller.setPlaybackSpeed(playbackSpeed); controller.setPlaybackSpeed(playbackSpeed);
String speedPref = String.format(Locale.US, "%.2f", 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); String speedStr = String.format("%.2fx", playbackSpeed);
txtvPlaybackSpeed.setText(speedStr); txtvPlaybackSpeed.setText(speedStr);
} else if (fromUser) { } else if (fromUser) {
float speed = Float.valueOf(UserPreferences.getPlaybackSpeed()); float speed = getCurrentSpeed();
barPlaybackSpeed.post(() -> barPlaybackSpeed.setProgress( barPlaybackSpeed.post(() -> barPlaybackSpeed.setProgress(
(int) ((speed - minPlaybackSpeed) / PLAYBACK_SPEED_STEP))); (int) ((speed - minPlaybackSpeed) / PLAYBACK_SPEED_STEP)));
} }
@ -188,7 +203,12 @@ public class PlaybackControlsDialog extends DialogFragment {
controller.setDownmix(isChecked); controller.setDownmix(isChecked);
} }
}); });
}
return dialog; private float getCurrentSpeed() {
if (isPlayingVideo) {
return UserPreferences.getVideoPlaybackSpeed();
}
return UserPreferences.getPlaybackSpeed();
} }
} }

View File

@ -596,7 +596,7 @@ public class QueueFragment extends Fragment {
String info = queue.size() + getString(R.string.episodes_suffix); String info = queue.size() + getString(R.string.episodes_suffix);
if(queue.size() > 0) { if(queue.size() > 0) {
long timeLeft = 0; long timeLeft = 0;
float playbackSpeed = Float.valueOf(UserPreferences.getPlaybackSpeed()); float playbackSpeed = UserPreferences.getPlaybackSpeed();
for(FeedItem item : queue) { for(FeedItem item : queue) {
if(item.getMedia() != null) { if(item.getMedia() != null) {
timeLeft += timeLeft +=

View File

@ -109,6 +109,7 @@ public class UserPreferences {
public static final String PREF_MEDIA_PLAYER = "prefMediaPlayer"; public static final String PREF_MEDIA_PLAYER = "prefMediaPlayer";
public static final String PREF_MEDIA_PLAYER_EXOPLAYER = "exoplayer"; public static final String PREF_MEDIA_PLAYER_EXOPLAYER = "exoplayer";
private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed"; private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed";
private static final String PREF_VIDEO_PLAYBACK_SPEED = "prefVideoPlaybackSpeed";
public static final String PREF_PLAYBACK_SKIP_SILENCE = "prefSkipSilence"; public static final String PREF_PLAYBACK_SKIP_SILENCE = "prefSkipSilence";
private static final String PREF_FAST_FORWARD_SECS = "prefFastForwardSecs"; private static final String PREF_FAST_FORWARD_SECS = "prefFastForwardSecs";
private static final String PREF_REWIND_SECS = "prefRewindSecs"; private static final String PREF_REWIND_SECS = "prefRewindSecs";
@ -319,8 +320,24 @@ public class UserPreferences {
return prefs.getBoolean(PREF_DELETE_REMOVES_FROM_QUEUE, false); return prefs.getBoolean(PREF_DELETE_REMOVES_FROM_QUEUE, false);
} }
public static String getPlaybackSpeed() { public static float getPlaybackSpeed() {
return prefs.getString(PREF_PLAYBACK_SPEED, "1.00"); try {
return Float.parseFloat(prefs.getString(PREF_PLAYBACK_SPEED, "1.00"));
} catch (NumberFormatException e) {
Log.e(TAG, Log.getStackTraceString(e));
UserPreferences.setPlaybackSpeed("1.00");
return 1.0f;
}
}
public static float getVideoPlaybackSpeed() {
try {
return Float.parseFloat(prefs.getString(PREF_VIDEO_PLAYBACK_SPEED, "1.00"));
} catch (NumberFormatException e) {
Log.e(TAG, Log.getStackTraceString(e));
UserPreferences.setVideoPlaybackSpeed("1.00");
return 1.0f;
}
} }
public static boolean isSkipSilence() { public static boolean isSkipSilence() {
@ -559,6 +576,12 @@ public class UserPreferences {
.apply(); .apply();
} }
public static void setVideoPlaybackSpeed(String speed) {
prefs.edit()
.putString(PREF_VIDEO_PLAYBACK_SPEED, speed)
.apply();
}
public static void setSkipSilence(boolean skipSilence) { public static void setSkipSilence(boolean skipSilence) {
prefs.edit() prefs.edit()
.putBoolean(PREF_PLAYBACK_SKIP_SILENCE, skipSilence) .putBoolean(PREF_PLAYBACK_SKIP_SILENCE, skipSilence)

View File

@ -213,8 +213,9 @@ public class PlayerWidgetJobService extends SafeJobIntentService {
private String getProgressString(int position, int duration) { private String getProgressString(int position, int duration) {
if (position > 0 && duration > 0) { if (position > 0 && duration > 0) {
position = TimeSpeedConverter.convert(position); TimeSpeedConverter converter = new TimeSpeedConverter(playbackService.getCurrentPlaybackSpeed());
duration = TimeSpeedConverter.convert(duration); position = converter.convert(position);
duration = converter.convert(duration);
return Converter.getDurationStringLong(position) + " / " return Converter.getDurationStringLong(position) + " / "
+ Converter.getDurationStringLong(duration); + Converter.getDurationStringLong(duration);
} else { } else {

View File

@ -303,14 +303,11 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
Log.d(TAG, "Audiofocus successfully requested"); Log.d(TAG, "Audiofocus successfully requested");
Log.d(TAG, "Resuming/Starting playback"); Log.d(TAG, "Resuming/Starting playback");
acquireWifiLockIfNecessary(); acquireWifiLockIfNecessary();
float speed = 1.0f; if (media.getMediaType() == MediaType.VIDEO) {
try { setPlaybackParams(UserPreferences.getVideoPlaybackSpeed(), UserPreferences.isSkipSilence());
speed = Float.parseFloat(UserPreferences.getPlaybackSpeed()); } else {
} catch(NumberFormatException e) { setPlaybackParams(UserPreferences.getPlaybackSpeed(), UserPreferences.isSkipSilence());
Log.e(TAG, Log.getStackTraceString(e));
UserPreferences.setPlaybackSpeed(String.valueOf(speed));
} }
setPlaybackParams(speed, UserPreferences.isSkipSilence());
setVolume(UserPreferences.getLeftVolume(), UserPreferences.getRightVolume()); setVolume(UserPreferences.getLeftVolume(), UserPreferences.getRightVolume());
if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) { if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) {
@ -601,11 +598,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
*/ */
@Override @Override
public boolean canSetSpeed() { public boolean canSetSpeed() {
boolean retVal = false; return mediaPlayer != null && mediaPlayer.canSetSpeed();
if (mediaPlayer != null && media != null && media.getMediaType() == MediaType.AUDIO) {
retVal = (mediaPlayer).canSetSpeed();
}
return retVal;
} }
/** /**
@ -614,13 +607,11 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
*/ */
private void setSpeedSyncAndSkipSilence(float speed, boolean skipSilence) { private void setSpeedSyncAndSkipSilence(float speed, boolean skipSilence) {
playerLock.lock(); playerLock.lock();
if (media != null && media.getMediaType() == MediaType.AUDIO) { if (mediaPlayer.canSetSpeed()) {
if (mediaPlayer.canSetSpeed()) { Log.d(TAG, "Playback speed was set to " + speed);
Log.d(TAG, "Playback speed was set to " + speed); callback.playbackSpeedChanged(speed);
callback.playbackSpeedChanged(speed);
}
mediaPlayer.setPlaybackParams(speed, skipSilence);
} }
mediaPlayer.setPlaybackParams(speed, skipSilence);
playerLock.unlock(); playerLock.unlock();
} }
@ -667,10 +658,8 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
*/ */
private void setVolumeSync(float volumeLeft, float volumeRight) { private void setVolumeSync(float volumeLeft, float volumeRight) {
playerLock.lock(); playerLock.lock();
if (media != null && media.getMediaType() == MediaType.AUDIO) { mediaPlayer.setVolume(volumeLeft, volumeRight);
mediaPlayer.setVolume(volumeLeft, volumeRight); Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight);
Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight);
}
playerLock.unlock(); playerLock.unlock();
} }

View File

@ -3,18 +3,19 @@ package de.danoeh.antennapod.core.util;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
public class TimeSpeedConverter { public class TimeSpeedConverter {
private TimeSpeedConverter() { private final float speed;
public TimeSpeedConverter(float speed) {
this.speed = speed;
} }
/** Convert millisecond according to the current playback speed /** Convert millisecond according to the current playback speed
* @param time: time to convert * @param time: time to convert
* @return converted time (can be < 0 if time is < 0) * @return converted time (can be < 0 if time is < 0)
*/ */
public static int convert(int time) { public int convert(int time) {
boolean timeRespectsSpeed = UserPreferences.timeRespectsSpeed(); boolean timeRespectsSpeed = UserPreferences.timeRespectsSpeed();
if (time > 0 && timeRespectsSpeed) { if (time > 0 && timeRespectsSpeed) {
float speed = Float.parseFloat(UserPreferences.getPlaybackSpeed());
return (int)(time / speed); return (int)(time / speed);
} }
return time; return time;

View File

@ -567,7 +567,8 @@ public class PlaybackController {
if (fromUser && playbackService != null && media != null) { if (fromUser && playbackService != null && media != null) {
float prog = progress / ((float) seekBar.getMax()); float prog = progress / ((float) seekBar.getMax());
int duration = media.getDuration(); int duration = media.getDuration();
int position = TimeSpeedConverter.convert((int) (prog * duration)); TimeSpeedConverter converter = new TimeSpeedConverter(playbackService.getCurrentPlaybackSpeed());
int position = converter.convert((int) (prog * duration));
txtvPosition.setText(Converter.getDurationStringLong(position)); txtvPosition.setText(Converter.getDurationStringLong(position));
return prog; return prog;
} }
@ -719,6 +720,7 @@ public class PlaybackController {
public boolean canSetPlaybackSpeed() { public boolean canSetPlaybackSpeed() {
return org.antennapod.audio.MediaPlayer.isPrestoLibraryInstalled(activity.getApplicationContext()) return org.antennapod.audio.MediaPlayer.isPrestoLibraryInstalled(activity.getApplicationContext())
|| UserPreferences.useSonic() || UserPreferences.useSonic()
|| UserPreferences.useExoplayer()
|| Build.VERSION.SDK_INT >= 23 || Build.VERSION.SDK_INT >= 23
|| (playbackService != null && playbackService.canSetSpeed()); || (playbackService != null && playbackService.canSetSpeed());
} }
@ -741,7 +743,7 @@ public class PlaybackController {
} }
public float getCurrentPlaybackSpeedMultiplier() { public float getCurrentPlaybackSpeedMultiplier() {
if (canSetPlaybackSpeed()) { if (playbackService != null && canSetPlaybackSpeed()) {
return playbackService.getCurrentPlaybackSpeed(); return playbackService.getCurrentPlaybackSpeed();
} else { } else {
return -1; return -1;