Variable speed playback as user preferences and customization of playback speed.

This commit is contained in:
James Falcon 2013-05-09 16:02:15 -05:00
parent b48c3807fd
commit 916074957f
9 changed files with 208 additions and 115 deletions

View File

@ -40,15 +40,25 @@
</string-array>
<string-array name="playback_speed_values">
<item>1.0</item>
<item>1.05</item>
<item>1.10</item>
<item>1.15</item>
<item>1.20</item>
<item>1.25</item>
<item>1.30</item>
<item>1.35</item>
<item>1.40</item>
<item>1.45</item>
<item>1.50</item>
<item>1.55</item>
<item>1.60</item>
<item>1.65</item>
<item>1.70</item>
<item>1.75</item>
<item>1.80</item>
<item>1.85</item>
<item>1.90</item>
<item>1.95</item>
<item>2.0</item>
</string-array>

View File

@ -147,7 +147,7 @@
<string name="download_plugin_label">Download Plugin</string>
<string name="no_playback_plugin_title">Plugin Not Installed</string>
<string name="no_playback_plugin_msg">For variable speed playback to work, a third party library must be installed.\n\nTap \'Download Plugin\' to download a free plugin from the Play Store\n\nAny problems found using this plugin are not the responsibility of AntennaPod and should be reported to the plugin owner.</string>
<string name="set_playback_speed_label">Playback Speed</string>
<string name="set_playback_speed_label">Playback Speeds</string>
<!-- Empty list labels -->
<string name="no_items_label">There are no items in this list.</string>
@ -193,8 +193,8 @@
<string name="pref_update_interval_hours_plural">hours</string>
<string name="pref_update_interval_hours_singular">hour</string>
<string name="pref_update_interval_hours_manual">Manual</string>
<string name="pref_playback_speed_title">Playback Speed</string>
<string name="pref_playback_speed_sum">Change the speed of audio playback using a 3rd party control</string>
<string name="pref_playback_speed_title">Playback Speeds</string>
<string name="pref_playback_speed_sum">Customize the speeds available for variable speed audio playback</string>
<!-- Search -->

View File

@ -10,6 +10,7 @@ import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
@ -24,11 +25,13 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.ChapterListAdapter;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
import de.danoeh.antennapod.feed.Chapter;
import de.danoeh.antennapod.feed.MediaType;
import de.danoeh.antennapod.feed.SimpleChapter;
import de.danoeh.antennapod.fragment.CoverFragment;
import de.danoeh.antennapod.fragment.ItemDescriptionFragment;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.util.playback.ExternalMedia;
import de.danoeh.antennapod.util.playback.Playable;
@ -400,27 +403,45 @@ public class AudioplayerActivity extends MediaplayerActivity {
@Override
public void onClick(View v) {
final double PLAYBACK_SPEED_STEP = 0.5;
final double PLAYBACK_SPEED_MAX = 2.0;
final double PLAYBACK_SPEED_DEFAULT = 1.0;
if (controller != null && controller.canSetPlaybackSpeed()) {
double currentPlaybackSpeed = controller
.getCurrentPlaybackSpeedMultiplier();
if (currentPlaybackSpeed != -1) {
if (currentPlaybackSpeed >= PLAYBACK_SPEED_MAX) {
controller.setPlaybackSpeed(PLAYBACK_SPEED_DEFAULT);
} else {
controller.setPlaybackSpeed(currentPlaybackSpeed
+ PLAYBACK_SPEED_STEP);
}
String[] availableSpeeds = UserPreferences
.getPlaybackSpeedArray();
String currentSpeed = UserPreferences.getPlaybackSpeed();
// Provide initial value in case the speed list has changed
// out from under us
// and our current speed isn't in the new list
String newSpeed;
if (availableSpeeds.length > 0) {
newSpeed = availableSpeeds[0];
} else {
controller.setPlaybackSpeed(PLAYBACK_SPEED_DEFAULT);
newSpeed = "1.0";
}
for (int i = 0; i < availableSpeeds.length; i++) {
if (availableSpeeds[i].equals(currentSpeed)) {
if (i == availableSpeeds.length - 1) {
newSpeed = availableSpeeds[0];
} else {
newSpeed = availableSpeeds[i + 1];
}
break;
}
}
UserPreferences.setPlaybackSpeed(AudioplayerActivity.this,
newSpeed);
controller.setPlaybackSpeed(Float.parseFloat(newSpeed));
}
}
});
butPlaybackSpeed.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
VariableSpeedDialog.showDialog(AudioplayerActivity.this);
return true;
}
});
}
@Override
@ -430,14 +451,12 @@ public class AudioplayerActivity extends MediaplayerActivity {
}
private void updateButPlaybackSpeed() {
double playbackSpeed;
if (controller == null
|| (playbackSpeed = controller
.getCurrentPlaybackSpeedMultiplier()) == -1) {
|| (controller.getCurrentPlaybackSpeedMultiplier() == -1)) {
butPlaybackSpeed.setVisibility(View.GONE);
} else {
butPlaybackSpeed.setVisibility(View.VISIBLE);
butPlaybackSpeed.setText(String.format("%.1fx", playbackSpeed));
butPlaybackSpeed.setText(UserPreferences.getPlaybackSpeed());
}
}

View File

@ -5,9 +5,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources.Theme;
import android.net.wifi.WifiConfiguration;
@ -29,7 +27,7 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FlattrClickWorker;
import de.danoeh.antennapod.asynctask.OpmlExportWorker;
import de.danoeh.antennapod.dialog.GetSpeedPlaybackPlugin;
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.util.flattr.FlattrUtils;
@ -175,7 +173,7 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
setPlaybackSpeed();
VariableSpeedDialog.showDialog(PreferenceActivity.this);
return true;
}
});
@ -264,40 +262,6 @@ public class PreferenceActivity extends SherlockPreferenceActivity {
}
}
private void setPlaybackSpeed() {
if (com.aocate.media.MediaPlayer
.isPrestoLibraryInstalled(PreferenceActivity.this)) {
int currentIndex = 0;
final String[] speedValues = getResources().getStringArray(
R.array.playback_speed_values);
for (int i = 0; i < speedValues.length; i++) {
// Probably shouldn't float compare here...
if (Float.parseFloat(speedValues[i]) == UserPreferences
.getPlaybackSpeed()) {
currentIndex = i;
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(
PreferenceActivity.this);
builder.setTitle(R.string.set_playback_speed_label);
builder.setSingleChoiceItems(R.array.playback_speed_values,
currentIndex, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UserPreferences.setPlaybackSpeed(
PreferenceActivity.this,
Float.valueOf(speedValues[which]));
dialog.dismiss();
}
});
builder.create().show();
} else {
GetSpeedPlaybackPlugin.showDialog(this);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;

View File

@ -1,31 +0,0 @@
package de.danoeh.antennapod.dialog;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import de.danoeh.antennapod.R;
public class GetSpeedPlaybackPlugin {
private GetSpeedPlaybackPlugin() {
}
public static void showDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.no_playback_plugin_title);
builder.setMessage(R.string.no_playback_plugin_msg);
builder.setNegativeButton(R.string.close_label, null);
builder.setPositiveButton(R.string.download_plugin_label,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent playStoreIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=com.falconware.prestissimo"));
context.startActivity(playStoreIntent);
}
});
builder.create().show();
}
}

View File

@ -0,0 +1,95 @@
package de.danoeh.antennapod.dialog;
import java.util.Arrays;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.preferences.UserPreferences;
public class VariableSpeedDialog {
private VariableSpeedDialog() {
}
public static void showDialog(final Context context) {
if (com.aocate.media.MediaPlayer.isPrestoLibraryInstalled(context)) {
showSpeedSelectorDialog(context);
} else {
showGetPluginDialog(context);
}
}
private static void showGetPluginDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.no_playback_plugin_title);
builder.setMessage(R.string.no_playback_plugin_msg);
builder.setNegativeButton(R.string.close_label, null);
builder.setPositiveButton(R.string.download_plugin_label,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent playStoreIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=com.falconware.prestissimo"));
context.startActivity(playStoreIntent);
}
});
builder.create().show();
}
private static void showSpeedSelectorDialog(final Context context) {
final String[] speedValues = context.getResources().getStringArray(
R.array.playback_speed_values);
// According to Java spec these get initialized to false on creation
final boolean[] speedChecked = new boolean[speedValues.length];
// Build the "isChecked" array so that multiChoice dialog is
// populated correctly
List<String> selectedSpeedList = Arrays.asList(UserPreferences
.getPlaybackSpeedArray());
for (int i = 0; i < speedValues.length; i++) {
speedChecked[i] = selectedSpeedList.contains(speedValues[i]);
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.set_playback_speed_label);
builder.setMultiChoiceItems(R.array.playback_speed_values,
speedChecked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
speedChecked[which] = isChecked;
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int choiceCount = 0;
for (int i = 0; i < speedChecked.length; i++) {
if (speedChecked[i]) {
choiceCount++;
}
}
String[] newSpeedValues = new String[choiceCount];
int newSpeedIndex = 0;
for (int i = 0; i < speedChecked.length; i++) {
if (speedChecked[i]) {
newSpeedValues[newSpeedIndex++] = speedValues[i];
}
}
UserPreferences.setPlaybackSpeedArray(context,
newSpeedValues);
}
});
builder.create().show();
}
}

View File

@ -5,6 +5,8 @@ import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.AlarmManager;
import android.app.PendingIntent;
@ -17,7 +19,6 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.OpmlImportFromPathActivity;
import de.danoeh.antennapod.receiver.FeedUpdateReceiver;
import de.danoeh.antennapod.service.PlaybackService;
/**
* Provides access to preferences set by the user in the settings screen. A
@ -43,6 +44,7 @@ public class UserPreferences implements
private static final String PREF_AUTODL_SELECTED_NETWORKS = "prefAutodownloadSelectedNetworks";
public static final String PREF_EPISODE_CACHE_SIZE = "prefEpisodeCacheSize";
private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed";
private static final String PREF_PLAYBACK_SPEED_ARRAY = "prefPlaybackSpeedArray";
private static int EPISODE_CACHE_SIZE_UNLIMITED = -1;
@ -62,7 +64,8 @@ public class UserPreferences implements
private boolean enableAutodownloadWifiFilter;
private String[] autodownloadSelectedNetworks;
private int episodeCacheSize;
private float playbackSpeed;
private String playbackSpeed;
private String[] playbackSpeedArray;
private UserPreferences(Context context) {
this.context = context;
@ -111,7 +114,9 @@ public class UserPreferences implements
episodeCacheSize = readEpisodeCacheSize(sp.getString(
PREF_EPISODE_CACHE_SIZE, "20"));
enableAutodownload = sp.getBoolean(PREF_ENABLE_AUTODL, false);
playbackSpeed = sp.getFloat(PREF_PLAYBACK_SPEED, (float) 1.0);
playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0");
playbackSpeedArray = readPlaybackSpeedArray(sp.getString(
PREF_PLAYBACK_SPEED_ARRAY, null));
}
private int readThemeValue(String valueFromPrefs) {
@ -139,6 +144,28 @@ public class UserPreferences implements
}
}
private String[] readPlaybackSpeedArray(String valueFromPrefs) {
String[] playbackSpeeds = null;
// If this preference hasn't been set yet, return all options
if (valueFromPrefs == null) {
playbackSpeeds = context.getResources().getStringArray(
R.array.playback_speed_values);
} else {
try {
JSONArray jsonArray = new JSONArray(valueFromPrefs);
playbackSpeeds = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
playbackSpeeds[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
Log.e(TAG,
"Got JSON error when trying to get speeds from JSONArray");
e.printStackTrace();
}
}
return playbackSpeeds;
}
private static void instanceAvailable() {
if (instance == null) {
throw new IllegalStateException(
@ -200,11 +227,16 @@ public class UserPreferences implements
return EPISODE_CACHE_SIZE_UNLIMITED;
}
public static float getPlaybackSpeed() {
public static String getPlaybackSpeed() {
instanceAvailable();
return instance.playbackSpeed;
}
public static String[] getPlaybackSpeedArray() {
instanceAvailable();
return instance.playbackSpeedArray;
}
/**
* Returns the capacity of the episode cache. This method will return the
* negative integer EPISODE_CACHE_SIZE_UNLIMITED if the cache size is set to
@ -260,20 +292,31 @@ public class UserPreferences implements
} else if (key.equals(PREF_ENABLE_AUTODL)) {
enableAutodownload = sp.getBoolean(PREF_ENABLE_AUTODL, false);
} else if (key.equals(PREF_PLAYBACK_SPEED)) {
playbackSpeed = sp.getFloat(PREF_PLAYBACK_SPEED, (float) 1.0);
playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0");
} else if (key.equals(PREF_PLAYBACK_SPEED_ARRAY)) {
playbackSpeedArray = readPlaybackSpeedArray(sp.getString(
PREF_PLAYBACK_SPEED_ARRAY, null));
}
}
public static void setPlaybackSpeed(Context context, float speed) {
public static void setPlaybackSpeed(Context context, String speed) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat(PREF_PLAYBACK_SPEED, speed);
editor.putString(PREF_PLAYBACK_SPEED, speed);
editor.apply();
}
Intent intent = new Intent(context, PlaybackService.class);
intent.putExtra(PlaybackService.EXTRA_PLAYBACK_SPEED, speed);
context.startService(intent);
public static void setPlaybackSpeedArray(Context context, String[] speeds) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
JSONArray jsonArray = new JSONArray();
for (String speed : speeds) {
jsonArray.put(speed);
}
editor.putString(PREF_PLAYBACK_SPEED_ARRAY, jsonArray.toString());
editor.apply();
}
public static void setAutodownloadSelectedNetworks(Context context,

View File

@ -82,7 +82,6 @@ public class PlaybackService extends Service {
public static final String ACTION_PLAYER_NOTIFICATION = "action.de.danoeh.antennapod.service.playerNotification";
public static final String EXTRA_NOTIFICATION_CODE = "extra.de.danoeh.antennapod.service.notificationCode";
public static final String EXTRA_NOTIFICATION_TYPE = "extra.de.danoeh.antennapod.service.notificationType";
public static final String EXTRA_PLAYBACK_SPEED = "extra.de.danoeh.antennapod.service.playbackSpeed";
/**
* If the PlaybackService receives this action, it will stop playback and
@ -373,17 +372,10 @@ public class PlaybackService extends Service {
if (AppConfig.DEBUG)
Log.d(TAG, "OnStartCommand called");
int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1);
float playbackSpeed = intent.getFloatExtra(EXTRA_PLAYBACK_SPEED, -1);
if (keycode != -1) {
if (AppConfig.DEBUG)
Log.d(TAG, "Received media button event");
handleKeycode(keycode);
} else if (playbackSpeed > 0) {
if (media == null) {
stopSelf();
} else {
setSpeed(playbackSpeed);
}
} else {
Playable playable = intent.getParcelableExtra(EXTRA_PLAYABLE);
@ -988,7 +980,7 @@ public class PlaybackService extends Service {
Log.d(TAG, "Resuming/Starting playback");
writePlaybackPreferences();
setSpeed(UserPreferences.getPlaybackSpeed());
setSpeed(Float.parseFloat(UserPreferences.getPlaybackSpeed()));
player.start();
if (status != PlayerStatus.PAUSED) {
player.seekTo(media.getPosition());
@ -1569,7 +1561,7 @@ public class PlaybackService extends Service {
}
}
public double getCurrentPlaybackSpeed() {
public float getCurrentPlaybackSpeed() {
if (media.getMediaType() == MediaType.AUDIO
&& player instanceof AudioPlayer) {
AudioPlayer audioPlayer = (AudioPlayer) player;

View File

@ -255,7 +255,8 @@ public abstract class PlaybackController {
public abstract void onPositionObserverUpdate();
private ServiceConnection mConnection = new ServiceConnection() {
private final ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
playbackService = ((PlaybackService.LocalBinder) service)
.getService();
@ -343,7 +344,7 @@ public abstract class PlaybackController {
};
private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
private final BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@ -356,7 +357,7 @@ public abstract class PlaybackController {
}
}
};
public abstract void onPlaybackSpeedChange();
public abstract void onShutdownNotification();
@ -662,13 +663,13 @@ public abstract class PlaybackController {
return playbackService != null && playbackService.canSetSpeed();
}
public void setPlaybackSpeed(double speed) {
public void setPlaybackSpeed(float speed) {
if (playbackService != null) {
playbackService.setSpeed(speed);
}
}
public double getCurrentPlaybackSpeedMultiplier() {
public float getCurrentPlaybackSpeedMultiplier() {
if (canSetPlaybackSpeed()) {
return playbackService.getCurrentPlaybackSpeed();
} else {