Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background playing - setting to ignore audio focus #1095

Open
wants to merge 1 commit into
base: edge
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions app/src/main/java/github/daneren2005/dsub/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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;
Expand All @@ -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);
Expand Down