Add dedicated EPII method to set volume

This means the volume is available in the internal player,
which is a preparation step to moving the audio focus
handling to the internal player too.

PiperOrigin-RevId: 726880098
(cherry picked from commit ef9b6d212e74c304730161434200244183ae23e6)
This commit is contained in:
tonihei 2025-02-14 04:57:41 -08:00
parent 8bd1db5f2c
commit be76766f95
3 changed files with 30 additions and 5 deletions

View File

@ -37,7 +37,6 @@ import static androidx.media3.exoplayer.Renderer.MSG_SET_SKIP_SILENCE_ENABLED;
import static androidx.media3.exoplayer.Renderer.MSG_SET_VIDEO_EFFECTS;
import static androidx.media3.exoplayer.Renderer.MSG_SET_VIDEO_FRAME_METADATA_LISTENER;
import static androidx.media3.exoplayer.Renderer.MSG_SET_VIDEO_OUTPUT_RESOLUTION;
import static androidx.media3.exoplayer.Renderer.MSG_SET_VOLUME;
import static java.lang.Math.max;
import static java.lang.Math.min;
@ -1538,7 +1537,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
return;
}
this.volume = volume;
sendVolumeToRenderers();
sendVolumeToInternalPlayer();
float finalVolume = volume;
listeners.sendEvent(EVENT_VOLUME_CHANGED, listener -> listener.onVolumeChanged(finalVolume));
}
@ -2737,9 +2736,9 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
}
private void sendVolumeToRenderers() {
private void sendVolumeToInternalPlayer() {
float scaledVolume = volume * audioFocusManager.getVolumeMultiplier();
sendRendererMessage(TRACK_TYPE_AUDIO, MSG_SET_VOLUME, scaledVolume);
internalPlayer.setVolume(scaledVolume);
}
private void updatePlayWhenReady(
@ -3224,7 +3223,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override
public void setVolumeMultiplier(float volumeMultiplier) {
sendVolumeToRenderers();
sendVolumeToInternalPlayer();
}
@Override

View File

@ -162,6 +162,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
private static final int MSG_PREPARE = 29;
private static final int MSG_SET_VIDEO_OUTPUT = 30;
private static final int MSG_SET_AUDIO_ATTRIBUTES = 31;
private static final int MSG_SET_VOLUME = 32;
private static final long BUFFERING_MAXIMUM_INTERVAL_MS =
Util.usToMs(Renderer.DEFAULT_DURATION_TO_PROGRESS_US);
@ -455,6 +456,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
handler.obtainMessage(MSG_SET_AUDIO_ATTRIBUTES, audioAttributes).sendToTarget();
}
public void setVolume(float volume) {
handler.obtainMessage(MSG_SET_VOLUME, volume).sendToTarget();
}
@Override
public synchronized void sendMessage(PlayerMessage message) {
if (released || !playbackLooper.getThread().isAlive()) {
@ -675,6 +680,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
case MSG_SET_AUDIO_ATTRIBUTES:
setAudioAttributesInternal((AudioAttributes) msg.obj);
break;
case MSG_SET_VOLUME:
setVolumeInternal((Float) msg.obj);
break;
case MSG_RELEASE:
releaseInternal();
// Return immediately to not send playback info updates after release.
@ -948,6 +956,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
trackSelector.setAudioAttributes(audioAttributes);
}
private void setVolumeInternal(float volume) throws ExoPlaybackException {
for (RendererHolder renderer : renderers) {
renderer.setVolume(volume);
}
}
private void notifyTrackSelectionPlayWhenReadyChanged(boolean playWhenReady) {
MediaPeriodHolder periodHolder = queue.getPlayingPeriod();
while (periodHolder != null) {

View File

@ -15,6 +15,7 @@
*/
package androidx.media3.exoplayer;
import static androidx.media3.common.C.TRACK_TYPE_AUDIO;
import static androidx.media3.common.C.TRACK_TYPE_VIDEO;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
@ -774,6 +775,17 @@ import java.util.Objects;
}
}
/** Sets the volume on the renderer. */
public void setVolume(float volume) throws ExoPlaybackException {
if (getTrackType() != TRACK_TYPE_AUDIO) {
return;
}
primaryRenderer.handleMessage(Renderer.MSG_SET_VOLUME, volume);
if (secondaryRenderer != null) {
secondaryRenderer.handleMessage(Renderer.MSG_SET_VOLUME, volume);
}
}
public boolean isRendererEnabled() {
boolean checkPrimary =
prewarmingState == RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_PRIMARY