From 5e6ed80c1cdd320c3e416afe701cd3661660ce40 Mon Sep 17 00:00:00 2001 From: tcd Date: Wed, 6 Apr 2022 00:12:42 +0200 Subject: [PATCH] setting to ignore audio focus --- .../github/daneren2005/dsub/util/Util.java | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/github/daneren2005/dsub/util/Util.java b/app/src/main/java/github/daneren2005/dsub/util/Util.java index 1aa17c35b..f2ef4b361 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/Util.java +++ b/app/src/main/java/github/daneren2005/dsub/util/Util.java @@ -1342,21 +1342,27 @@ public static void unregisterMediaButtonEventReceiver(Context context) { @TargetApi(8) public static void requestAudioFocus(final Context context, final AudioManager audioManager) { - if(Build.VERSION.SDK_INT >= 26) { - if(audioFocusRequest == null) { - AudioAttributes playbackAttributes = new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build(); - - audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) - .setAudioAttributes(playbackAttributes) - .setOnAudioFocusChangeListener(getAudioFocusChangeListener(context, audioManager)) - .setWillPauseWhenDucked(true) - .build(); - audioManager.requestAudioFocus(audioFocusRequest); + SharedPreferences prefs = getPreferences(context); + int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); + + if(Build.VERSION.SDK_INT >= 26) { + // don't request audio focus if lossPref is 3 (don't use audiofocus) + if(audioFocusRequest == null && lossPref != 3) { + AudioAttributes playbackAttributes = new AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build(); + + audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) + .setAudioAttributes(playbackAttributes) + .setOnAudioFocusChangeListener(getAudioFocusChangeListener(context, audioManager)) + .setWillPauseWhenDucked(true) + .build(); + audioManager.requestAudioFocus(audioFocusRequest); + + } - } else if (Build.VERSION.SDK_INT >= 8 && focusListener == null) { + } else if (Build.VERSION.SDK_INT >= 8 && focusListener == null && lossPref != 3) { audioManager.requestAudioFocus(focusListener = getAudioFocusChangeListener(context, audioManager), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } } @@ -1365,10 +1371,10 @@ private static OnAudioFocusChangeListener getAudioFocusChangeListener(final Cont return new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadService downloadService = (DownloadService)context; + SharedPreferences prefs = getPreferences(context); if((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isRemoteEnabled()) { if(downloadService.getPlayerState() == PlayerState.STARTED) { Log.i(TAG, "Temporary loss of focus"); - SharedPreferences prefs = getPreferences(context); int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); if(lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; @@ -1390,7 +1396,11 @@ public void onAudioFocusChange(int focusChange) { } else if(focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isRemoteEnabled()) { Log.i(TAG, "Permanently lost focus"); focusListener = null; - downloadService.pause(); + int lossPref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); + // if setting is "do nothing" - ignore audio focus switch because user wants background play + if (lossPref != 3) { + downloadService.pause(); + } if(audioFocusRequest != null && Build.VERSION.SDK_INT >= 26) { audioManager.abandonAudioFocusRequest(audioFocusRequest);