diff options
author | Scott Jackson <daneren2005@gmail.com> | 2012-11-12 09:54:22 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2012-11-12 09:54:22 -0800 |
commit | 3a64650fa51b5caf330bcd8b0fa6d8e514397e6f (patch) | |
tree | 8f5e25ba292c34e1683db602f5ca63eb77df98de /subsonic-android/src | |
parent | 4e4dc1bb43066da355da90c1f2c220a9b4e56e23 (diff) | |
download | dsub-3a64650fa51b5caf330bcd8b0fa6d8e514397e6f.tar.gz dsub-3a64650fa51b5caf330bcd8b0fa6d8e514397e6f.tar.bz2 dsub-3a64650fa51b5caf330bcd8b0fa6d8e514397e6f.zip |
Added option to either pause or lower volume on transient loss of audio focus
Diffstat (limited to 'subsonic-android/src')
3 files changed, 22 insertions, 4 deletions
diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java index febfedca..82b3cc05 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java @@ -58,6 +58,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer private ListPreference preloadCount; private EditTextPreference randomSize; private EditTextPreference sleepTimerDuration; + private ListPreference tempLoss; @Override public void onCreate(Bundle savedInstanceState) { @@ -73,6 +74,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer preloadCount = (ListPreference) findPreference(Constants.PREFERENCES_KEY_PRELOAD_COUNT); randomSize = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_RANDOM_SIZE); sleepTimerDuration = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_SLEEP_TIMER_DURATION); + tempLoss = (ListPreference) findPreference(Constants.PREFERENCES_KEY_TEMP_LOSS); findPreference("testConnection1").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override @@ -161,6 +163,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer preloadCount.setSummary(preloadCount.getEntry()); randomSize.setSummary(randomSize.getText()); sleepTimerDuration.setSummary(sleepTimerDuration.getText()); + tempLoss.setSummary(tempLoss.getEntry()); for (ServerSettings ss : serverSettings.values()) { ss.update(); } diff --git a/subsonic-android/src/github/daneren2005/dsub/util/Constants.java b/subsonic-android/src/github/daneren2005/dsub/util/Constants.java index 2d67d3c9..eb0a15fd 100644 --- a/subsonic-android/src/github/daneren2005/dsub/util/Constants.java +++ b/subsonic-android/src/github/daneren2005/dsub/util/Constants.java @@ -79,6 +79,7 @@ public final class Constants { public static final String PREFERENCES_KEY_SLEEP_TIMER = "sleepTimer"; public static final String PREFERENCES_KEY_SLEEP_TIMER_DURATION = "sleepTimerDuration"; public static final String PREFERENCES_KEY_OFFLINE = "offline"; + public static final String PREFERENCES_KEY_TEMP_LOSS = "tempLoss"; // Name of the preferences file. public static final String PREFERENCES_FILE_NAME = "github.daneren2005.dsub_preferences"; diff --git a/subsonic-android/src/github/daneren2005/dsub/util/Util.java b/subsonic-android/src/github/daneren2005/dsub/util/Util.java index b6ad69bb..53bc54c4 100644 --- a/subsonic-android/src/github/daneren2005/dsub/util/Util.java +++ b/subsonic-android/src/github/daneren2005/dsub/util/Util.java @@ -99,6 +99,8 @@ public final class Util { public static final String EVENT_PLAYSTATE_CHANGED = "github.daneren2005.dsub.EVENT_PLAYSTATE_CHANGED"; private static boolean pauseFocus = false; + private static int currentVolume = 0; + private static int lossPref = 0; private static final Map<Integer, Version> SERVER_REST_VERSIONS = new ConcurrentHashMap<Integer, Version>(); @@ -746,20 +748,32 @@ public final class Util { @TargetApi(8) public static void requestAudioFocus(final Context context) { - if (Build.VERSION.SDK_INT >= 8) { - AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + if (Build.VERSION.SDK_INT >= 8) { + final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadServiceImpl downloadService = (DownloadServiceImpl)context; if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { if(downloadService.getPlayerState() == PlayerState.STARTED) { pauseFocus = true; - downloadService.pause(); + + SharedPreferences prefs = getPreferences(context); + lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "0")); + if(lossPref > 0) { + currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); + audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int)Math.ceil(currentVolume / (double)lossPref), 0); + } else { + downloadService.pause(); + } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if(pauseFocus) { pauseFocus = false; - downloadService.start(); + if(lossPref > 0) { + audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0); + } else { + downloadService.start(); + } } } } |