Allow different playback speed for video
This commit is contained in:
parent
a445942cec
commit
c14c223e2f
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 +=
|
||||
|
|
|
@ -109,6 +109,7 @@ public class UserPreferences {
|
|||
public static final String PREF_MEDIA_PLAYER = "prefMediaPlayer";
|
||||
public static final String PREF_MEDIA_PLAYER_EXOPLAYER = "exoplayer";
|
||||
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";
|
||||
private static final String PREF_FAST_FORWARD_SECS = "prefFastForwardSecs";
|
||||
private static final String PREF_REWIND_SECS = "prefRewindSecs";
|
||||
|
@ -319,8 +320,24 @@ public class UserPreferences {
|
|||
return prefs.getBoolean(PREF_DELETE_REMOVES_FROM_QUEUE, false);
|
||||
}
|
||||
|
||||
public static String getPlaybackSpeed() {
|
||||
return prefs.getString(PREF_PLAYBACK_SPEED, "1.00");
|
||||
public static float getPlaybackSpeed() {
|
||||
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() {
|
||||
|
@ -559,6 +576,12 @@ public class UserPreferences {
|
|||
.apply();
|
||||
}
|
||||
|
||||
public static void setVideoPlaybackSpeed(String speed) {
|
||||
prefs.edit()
|
||||
.putString(PREF_VIDEO_PLAYBACK_SPEED, speed)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static void setSkipSilence(boolean skipSilence) {
|
||||
prefs.edit()
|
||||
.putBoolean(PREF_PLAYBACK_SKIP_SILENCE, skipSilence)
|
||||
|
|
|
@ -213,8 +213,9 @@ public class PlayerWidgetJobService extends SafeJobIntentService {
|
|||
|
||||
private String getProgressString(int position, int duration) {
|
||||
if (position > 0 && duration > 0) {
|
||||
position = TimeSpeedConverter.convert(position);
|
||||
duration = TimeSpeedConverter.convert(duration);
|
||||
TimeSpeedConverter converter = new TimeSpeedConverter(playbackService.getCurrentPlaybackSpeed());
|
||||
position = converter.convert(position);
|
||||
duration = converter.convert(duration);
|
||||
return Converter.getDurationStringLong(position) + " / "
|
||||
+ Converter.getDurationStringLong(duration);
|
||||
} else {
|
||||
|
|
|
@ -303,14 +303,11 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
|
|||
Log.d(TAG, "Audiofocus successfully requested");
|
||||
Log.d(TAG, "Resuming/Starting playback");
|
||||
acquireWifiLockIfNecessary();
|
||||
float speed = 1.0f;
|
||||
try {
|
||||
speed = Float.parseFloat(UserPreferences.getPlaybackSpeed());
|
||||
} catch(NumberFormatException e) {
|
||||
Log.e(TAG, Log.getStackTraceString(e));
|
||||
UserPreferences.setPlaybackSpeed(String.valueOf(speed));
|
||||
if (media.getMediaType() == MediaType.VIDEO) {
|
||||
setPlaybackParams(UserPreferences.getVideoPlaybackSpeed(), UserPreferences.isSkipSilence());
|
||||
} else {
|
||||
setPlaybackParams(UserPreferences.getPlaybackSpeed(), UserPreferences.isSkipSilence());
|
||||
}
|
||||
setPlaybackParams(speed, UserPreferences.isSkipSilence());
|
||||
setVolume(UserPreferences.getLeftVolume(), UserPreferences.getRightVolume());
|
||||
|
||||
if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) {
|
||||
|
@ -601,11 +598,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
|
|||
*/
|
||||
@Override
|
||||
public boolean canSetSpeed() {
|
||||
boolean retVal = false;
|
||||
if (mediaPlayer != null && media != null && media.getMediaType() == MediaType.AUDIO) {
|
||||
retVal = (mediaPlayer).canSetSpeed();
|
||||
}
|
||||
return retVal;
|
||||
return mediaPlayer != null && mediaPlayer.canSetSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -614,13 +607,11 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
|
|||
*/
|
||||
private void setSpeedSyncAndSkipSilence(float speed, boolean skipSilence) {
|
||||
playerLock.lock();
|
||||
if (media != null && media.getMediaType() == MediaType.AUDIO) {
|
||||
if (mediaPlayer.canSetSpeed()) {
|
||||
Log.d(TAG, "Playback speed was set to " + speed);
|
||||
callback.playbackSpeedChanged(speed);
|
||||
}
|
||||
mediaPlayer.setPlaybackParams(speed, skipSilence);
|
||||
if (mediaPlayer.canSetSpeed()) {
|
||||
Log.d(TAG, "Playback speed was set to " + speed);
|
||||
callback.playbackSpeedChanged(speed);
|
||||
}
|
||||
mediaPlayer.setPlaybackParams(speed, skipSilence);
|
||||
playerLock.unlock();
|
||||
}
|
||||
|
||||
|
@ -667,10 +658,8 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
|
|||
*/
|
||||
private void setVolumeSync(float volumeLeft, float volumeRight) {
|
||||
playerLock.lock();
|
||||
if (media != null && media.getMediaType() == MediaType.AUDIO) {
|
||||
mediaPlayer.setVolume(volumeLeft, volumeRight);
|
||||
Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight);
|
||||
}
|
||||
mediaPlayer.setVolume(volumeLeft, volumeRight);
|
||||
Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight);
|
||||
playerLock.unlock();
|
||||
}
|
||||
|
||||
|
|
|
@ -3,18 +3,19 @@ package de.danoeh.antennapod.core.util;
|
|||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
|
||||
public class TimeSpeedConverter {
|
||||
private TimeSpeedConverter() {
|
||||
private final float speed;
|
||||
|
||||
public TimeSpeedConverter(float speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
/** Convert millisecond according to the current playback speed
|
||||
* @param time: time to convert
|
||||
* @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();
|
||||
if (time > 0 && timeRespectsSpeed) {
|
||||
float speed = Float.parseFloat(UserPreferences.getPlaybackSpeed());
|
||||
return (int)(time / speed);
|
||||
}
|
||||
return time;
|
||||
|
|
|
@ -567,7 +567,8 @@ public class PlaybackController {
|
|||
if (fromUser && playbackService != null && media != null) {
|
||||
float prog = progress / ((float) seekBar.getMax());
|
||||
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));
|
||||
return prog;
|
||||
}
|
||||
|
@ -719,6 +720,7 @@ public class PlaybackController {
|
|||
public boolean canSetPlaybackSpeed() {
|
||||
return org.antennapod.audio.MediaPlayer.isPrestoLibraryInstalled(activity.getApplicationContext())
|
||||
|| UserPreferences.useSonic()
|
||||
|| UserPreferences.useExoplayer()
|
||||
|| Build.VERSION.SDK_INT >= 23
|
||||
|| (playbackService != null && playbackService.canSetSpeed());
|
||||
}
|
||||
|
@ -741,7 +743,7 @@ public class PlaybackController {
|
|||
}
|
||||
|
||||
public float getCurrentPlaybackSpeedMultiplier() {
|
||||
if (canSetPlaybackSpeed()) {
|
||||
if (playbackService != null && canSetPlaybackSpeed()) {
|
||||
return playbackService.getCurrentPlaybackSpeed();
|
||||
} else {
|
||||
return -1;
|
||||
|
|
Loading…
Reference in New Issue