Skip to content

Differ AudioManager retrieval to whenever AudioFocusManagement is req… #1616

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

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Changes from 1 commit
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
Next Next commit
Differ AudioManager retrieval to whenever AudioFocusManagement is req…
…uired
  • Loading branch information
colinkho authored and icbaker committed Aug 20, 2024
commit 6af92b0af3526fb3aa534a5c7c6c1eb2df1369dc
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ public interface PlayerControl {
private static final float VOLUME_MULTIPLIER_DUCK = 0.2f;
private static final float VOLUME_MULTIPLIER_DEFAULT = 1.0f;

private final AudioManager audioManager;
private final AudioFocusListener focusListener;
private final Context applicationContext;
@Nullable private PlayerControl playerControl;
@Nullable private AudioAttributes audioAttributes;

@Nullable private AudioManager audioManager;
private @AudioFocusState int audioFocusState;
private @AudioFocusGain int focusGainToRequest;
private float volumeMultiplier = VOLUME_MULTIPLIER_DEFAULT;
Expand All @@ -180,9 +180,7 @@ public interface PlayerControl {
* @param playerControl A {@link PlayerControl} to handle commands from this instance.
*/
public AudioFocusManager(Context context, Handler eventHandler, PlayerControl playerControl) {
this.audioManager =
checkNotNull(
(AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE));
this.applicationContext = context;
this.playerControl = playerControl;
this.focusListener = new AudioFocusListener(eventHandler);
this.audioFocusState = AUDIO_FOCUS_STATE_NOT_REQUESTED;
Expand Down Expand Up @@ -287,7 +285,7 @@ private void abandonAudioFocusIfHeld() {
}

private int requestAudioFocusDefault() {
return audioManager.requestAudioFocus(
return getAudioManager().requestAudioFocus(
focusListener,
Util.getStreamTypeForAudioUsage(checkNotNull(audioAttributes).usage),
focusGainToRequest);
Expand All @@ -312,17 +310,17 @@ private int requestAudioFocusV26() {

rebuildAudioFocusRequest = false;
}
return audioManager.requestAudioFocus(audioFocusRequest);
return getAudioManager().requestAudioFocus(audioFocusRequest);
}

private void abandonAudioFocusDefault() {
audioManager.abandonAudioFocus(focusListener);
getAudioManager().abandonAudioFocus(focusListener);
}

@RequiresApi(26)
private void abandonAudioFocusV26() {
if (audioFocusRequest != null) {
audioManager.abandonAudioFocusRequest(audioFocusRequest);
getAudioManager().abandonAudioFocusRequest(audioFocusRequest);
}
}

Expand Down Expand Up @@ -455,6 +453,13 @@ private void executePlayerCommand(@PlayerCommand int playerCommand) {
}
}

private AudioManager getAudioManager() {
if (audioManager == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIt: you can use Guava's Suppliers.memoize for this, to avoid manually needing to check for null, so change private final AudioManager audioManager; above to private final Supplier audioManager;, change the instantiation to:

this.audioManager =
        Suppliers.memoize(() -> checkNotNull(
            (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)));

And then change the usage sites from audioManager to audioManager.get()

Maybe change the field name to audioManagerSupplier too - up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. Will change accordingly

audioManager = (AudioManager) applicationContext.getSystemService(Context.AUDIO_SERVICE);
}
return audioManager;
}

// Internal audio focus listener.

private class AudioFocusListener implements AudioManager.OnAudioFocusChangeListener {
Expand Down