From ea9302aa0ad70e9bcfbb4eb4e1735762d59fb81a Mon Sep 17 00:00:00 2001 From: Xiao Bao Clark Date: Sun, 23 Aug 2015 09:38:09 +1000 Subject: [PATCH] Added ducking volume as a preference --- res/values/translatable.xml | 2 ++ res/xml/preference_audio.xml | 6 ++++ .../android/vanilla/PlaybackService.java | 16 +++++++++ .../android/vanilla/PrefKeys.java | 1 + .../android/vanilla/SeekBarPreference.java | 28 +++++++++++++--- .../android/vanilla/VanillaMediaPlayer.java | 33 +++++++++++++------ 6 files changed, 72 insertions(+), 14 deletions(-) diff --git a/res/values/translatable.xml b/res/values/translatable.xml index a3541d57..ede87f8c 100644 --- a/res/values/translatable.xml +++ b/res/values/translatable.xml @@ -150,6 +150,8 @@ THE SOFTWARE. Audio Volume + Volume During Notification + Playback volume: Headset/Bluetooth Controls This is also required for ICS lockscreen controls. Headset Control Beep diff --git a/res/xml/preference_audio.xml b/res/xml/preference_audio.xml index 4da7f060..3a2ec227 100644 --- a/res/xml/preference_audio.xml +++ b/res/xml/preference_audio.xml @@ -31,6 +31,12 @@ THE SOFTWARE. android:fragment="ch.blinkenlights.android.vanilla.PreferencesActivity$ReplayGainFragment" android:title="@string/replaygain" android:summary="@string/replaygain_summary" /> + 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)) { diff --git a/src/ch/blinkenlights/android/vanilla/PrefKeys.java b/src/ch/blinkenlights/android/vanilla/PrefKeys.java index 747783a4..12e366ce 100644 --- a/src/ch/blinkenlights/android/vanilla/PrefKeys.java +++ b/src/ch/blinkenlights/android/vanilla/PrefKeys.java @@ -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"; } diff --git a/src/ch/blinkenlights/android/vanilla/SeekBarPreference.java b/src/ch/blinkenlights/android/vanilla/SeekBarPreference.java index 56774074..b0ac54e8 100644 --- a/src/ch/blinkenlights/android/vanilla/SeekBarPreference.java +++ b/src/ch/blinkenlights/android/vanilla/SeekBarPreference.java @@ -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); + } } diff --git a/src/ch/blinkenlights/android/vanilla/VanillaMediaPlayer.java b/src/ch/blinkenlights/android/vanilla/VanillaMediaPlayer.java index a43bccc5..51d30f17 100644 --- a/src/ch/blinkenlights/android/vanilla/VanillaMediaPlayer.java +++ b/src/ch/blinkenlights/android/vanilla/VanillaMediaPlayer.java @@ -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); } - + }