Added ducking volume as a preference

This commit is contained in:
Xiao Bao Clark committed 2015-08-23 09:38:09 +10:00
1 parent 2c7a9d53cd
commit ea9302aa0a
6 files changed
+72 -14

No files matched your search

@@ -391,6 +391,10 @@ public final class PlaybackService extends Service
private boolean mReplayGainAlbumEnabled;
private int mReplayGainBump;
private int mReplayGainUntaggedDeBump;
/**
* Percentage to set the volume as while a notification is playing (aka ducking)
*/
private int mVolumeOnNotification;
/**
* TRUE if the readahead feature is enabled
*/
@@ -456,6 +460,9 @@ public final class PlaybackService extends Service
mReplayGainBump = settings.getInt(PrefKeys.REPLAYGAIN_BUMP, 75); /* seek bar is 150 -> 75 == middle == 0 */
mReplayGainUntaggedDeBump = settings.getInt(PrefKeys.REPLAYGAIN_UNTAGGED_DEBUMP, 150); /* seek bar is 150 -> == 0 */
mVolumeOnNotification = settings.getInt(PrefKeys.VOLUME_ON_NOTIFICATION, 50);
refreshDuckingValues();
mReadaheadEnabled = settings.getBoolean(PrefKeys.ENABLE_READAHEAD, false);
PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
@@ -628,6 +635,12 @@ public final class PlaybackService extends Service
applyReplayGain(mPreparedMediaPlayer);
}
private void refreshDuckingValues() {
float duckingFactor = ((float)mVolumeOnNotification)/100f;
mMediaPlayer.setDuckingFactor(duckingFactor);
mPreparedMediaPlayer.setDuckingFactor(duckingFactor);
}
/***
* Reads the replay gain value of the media players data source
* and adjusts the volume
@@ -849,6 +862,9 @@ public final class PlaybackService extends Service
} else if (PrefKeys.REPLAYGAIN_UNTAGGED_DEBUMP.equals(key)) {
mReplayGainUntaggedDeBump = settings.getInt(PrefKeys.REPLAYGAIN_UNTAGGED_DEBUMP, 150);
refreshReplayGainValues();
} else if (PrefKeys.VOLUME_ON_NOTIFICATION.equals(key)) {
mVolumeOnNotification = settings.getInt(PrefKeys.VOLUME_ON_NOTIFICATION, 50);
refreshDuckingValues();
} else if (PrefKeys.ENABLE_READAHEAD.equals(key)) {
mReadaheadEnabled = settings.getBoolean(PrefKeys.ENABLE_READAHEAD, false);
} else if (PrefKeys.USE_DARK_THEME.equals(key)) {
@@ -66,4 +66,5 @@ public class PrefKeys {
public static final String ENABLE_READAHEAD = "enable_readahead";
public static final String USE_DARK_THEME = "use_dark_theme";
public static final String FILESYSTEM_BROWSE_START = "filesystem_browse_start";
public static final String VOLUME_ON_NOTIFICATION = "volume_on_notification";
}
@@ -88,6 +88,9 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
} else if(PrefKeys.REPLAYGAIN_UNTAGGED_DEBUMP.equals(getKey())) {
String summary = (String)mContext.getResources().getText(R.string.replaygain_untagged_debump_summary);
return String.format("%s %.1fdB", summary, (value-150)/10f);
} else if (PrefKeys.VOLUME_ON_NOTIFICATION.equals(getKey())) {
String summary = mContext.getString(R.string.volume_on_notification_summary);
return summary + " " + value + "%";
} else {
return String.format("%d%% (%+.1fdB)", value, 20 * Math.log10(Math.pow(value / 100.0, 3)));
}
@@ -103,7 +106,17 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
SeekBar seekBar = (SeekBar)view.findViewById(R.id.seek_bar);
int maxValue = (PrefKeys.SHAKE_THRESHOLD.equals(getKey()) ? 300 : 150);
int maxValue;
switch (getKey()) {
case PrefKeys.SHAKE_THRESHOLD:
maxValue = 300;
break;
case PrefKeys.VOLUME_ON_NOTIFICATION:
maxValue = 100;
break;
default:
maxValue = 150;
}
seekBar.setMax(maxValue);
seekBar.setProgress(mValue);
@@ -115,6 +128,9 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
@Override
protected void onDialogClosed(boolean positiveResult)
{
if(!positiveResult && PrefKeys.VOLUME_ON_NOTIFICATION.equals(getKey())) {
setValue(50);
}
notifyChanged();
}
@@ -122,9 +138,7 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (fromUser) {
mValue = progress;
mValueText.setText(getSummary(progress));
persistInt(progress);
setValue(progress);
}
}
@@ -137,4 +151,10 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
public void onStopTrackingTouch(SeekBar seekBar)
{
}
private void setValue(int value) {
mValue = value;
mValueText.setText(getSummary(value));
persistInt(value);
}
}
@@ -31,6 +31,7 @@ public class VanillaMediaPlayer extends MediaPlayer {
private boolean mHasNextMediaPlayer;
private float mReplayGain = Float.NaN;
private float mDuckingFactor = Float.NaN;
private boolean mIsDucking = false;
/**
* Constructs a new VanillaMediaPlayer class
@@ -111,11 +112,11 @@ public class VanillaMediaPlayer extends MediaPlayer {
/**
* Sets the desired scaling due to replay gain.
* @param mReplayGain the factor to adjust the volume by. Must be between 0 and 1 (inclusive)
* @param replayGain the factor to adjust the volume by. Must be between 0 and 1 (inclusive)
* or {@link Float#NaN} to disable replay gain scaling
*/
public void setReplayGain(float mReplayGain) {
this.mReplayGain = mReplayGain;
public void setReplayGain(float replayGain) {
mReplayGain = replayGain;
updateVolume();
}
@@ -125,22 +126,34 @@ public class VanillaMediaPlayer extends MediaPlayer {
* @param isDucking true if we are ducking, false if we are not
*/
public void setIsDucking(boolean isDucking) {
mDuckingFactor = (isDucking ? 0.2f : Float.NaN);
mIsDucking = isDucking;
updateVolume();
}
/**
* Sets the volume. Ducking takes precedence over replay gain. If neither ducking nor replay
* gain is set, uses the default value of 1.0f
* Sets the desired scaling while ducking.
* @param duckingFactor the factor to adjust the volume by while ducking. Must be between 0
* and 1 (inclusive) or {@link Float#NaN} to disable ducking completely
*
* See also {@link #setIsDucking(boolean)}
*/
public void setDuckingFactor(float duckingFactor) {
mDuckingFactor = duckingFactor;
updateVolume();
}
/**
* Sets the volume, using the replay gain and ducking if appropriate
*/
private void updateVolume() {
float volume = 1.0f;
if(!Float.isNaN(mDuckingFactor)) {
volume = mDuckingFactor;
} else if (!Float.isNaN(mReplayGain)) {
if (!Float.isNaN(mReplayGain)) {
volume = mReplayGain;
}
if(mIsDucking && !Float.isNaN(mDuckingFactor)) {
volume *= mDuckingFactor;
}
setVolume(volume, volume);
}
}