这是indexloc提供的服务,不要输入任何密码
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
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
API 35+ (where the device advertises support for this).
* Handle preload callbacks asynchronously in `PreloadMediaSource`
([#1568](https://github.com/androidx/media/issues/1568)).
* Defer the blocking call to
`Context.getSystemService(Context.AUDIO_SERVICE)` until audio focus
handling is enabled. This ensures the blocking call isn't done if audio
focus handling is not enabled
([#1616](https://github.com/androidx/media/pull/1616)).
* Transformer:
* Add `SurfaceAssetLoader`, which supports queueing video data to
Transformer via a `Surface`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -160,15 +162,14 @@ 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 Supplier<AudioManager> audioManager;
private final AudioFocusListener focusListener;
@Nullable private PlayerControl playerControl;
@Nullable private AudioAttributes audioAttributes;

private @AudioFocusState int audioFocusState;
private @AudioFocusGain int focusGainToRequest;
private float volumeMultiplier = VOLUME_MULTIPLIER_DEFAULT;

private @MonotonicNonNull AudioFocusRequest audioFocusRequest;
private boolean rebuildAudioFocusRequest;

Expand All @@ -181,8 +182,11 @@ public interface PlayerControl {
*/
public AudioFocusManager(Context context, Handler eventHandler, PlayerControl playerControl) {
this.audioManager =
checkNotNull(
(AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE));
Suppliers.memoize(
() ->
checkNotNull(
(AudioManager)
context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)));
this.playerControl = playerControl;
this.focusListener = new AudioFocusListener(eventHandler);
this.audioFocusState = AUDIO_FOCUS_STATE_NOT_REQUESTED;
Expand Down Expand Up @@ -287,10 +291,12 @@ private void abandonAudioFocusIfHeld() {
}

private int requestAudioFocusDefault() {
return audioManager.requestAudioFocus(
focusListener,
Util.getStreamTypeForAudioUsage(checkNotNull(audioAttributes).usage),
focusGainToRequest);
return audioManager
.get()
.requestAudioFocus(
focusListener,
Util.getStreamTypeForAudioUsage(checkNotNull(audioAttributes).usage),
focusGainToRequest);
}

@RequiresApi(26)
Expand All @@ -312,17 +318,17 @@ private int requestAudioFocusV26() {

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

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

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

Expand Down