Commit c982f4c4 by kimvde Committed by Oliver Woodman

Add possibility to set the audio session id

Issue: #6975
PiperOrigin-RevId: 299328798
parent ab21f710
......@@ -19,6 +19,8 @@
* Add `Player.onPlayWhenReadyChanged` with reasons.
* Add `Player.onPlaybackStateChanged` and deprecate
`Player.onPlayerStateChanged`.
* Add `Player.setAudioSessionId` to set the session ID attached to the
`AudioTrack`.
* Deprecate and rename `getPlaybackError` to `getPlayerError` for
consistency.
* Deprecate and rename `onLoadingChanged` to `onIsLoadingChanged` for
......
......@@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2;
import android.content.Context;
import android.os.Looper;
import android.view.Surface;
import android.view.SurfaceHolder;
......@@ -123,6 +124,18 @@ public interface Player {
/** Returns the attributes for audio playback. */
AudioAttributes getAudioAttributes();
/**
* Sets the ID of the audio session to attach to the underlying {@link
* android.media.AudioTrack}.
*
* <p>The audio session ID can be generated using {@link C#generateAudioSessionIdV21(Context)}
* for API 21+.
*
* @param audioSessionId The audio session ID, or {@link C#AUDIO_SESSION_ID_UNSET} if it should
* be generated by the framework.
*/
void setAudioSessionId(int audioSessionId);
/** Returns the audio session identifier, or {@link C#AUDIO_SESSION_ID_UNSET} if not set. */
int getAudioSessionId();
......
......@@ -132,6 +132,12 @@ public interface Renderer extends PlayerMessage.Target {
*/
int MSG_SET_SKIP_SILENCE_ENABLED = 101;
/**
* A type of a message that can be passed to an audio renderer via {@link
* ExoPlayer#createMessage(Target)}. The message payload should be an {@link Integer} instance
* representing the audio session ID that will be attached to the underlying audio track.
*/
int MSG_SET_AUDIO_SESSION_ID = 102;
/**
* Applications or extensions may define custom {@code MSG_*} constants that can be passed to
* renderers. These custom constants must be greater than or equal to this value.
*/
......
......@@ -673,6 +673,21 @@ public class SimpleExoPlayer extends BasePlayer
}
@Override
public void setAudioSessionId(int audioSessionId) {
verifyApplicationThread();
this.audioSessionId = audioSessionId;
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) {
player
.createMessage(renderer)
.setType(Renderer.MSG_SET_AUDIO_SESSION_ID)
.setPayload(audioSessionId)
.send();
}
}
}
@Override
public int getAudioSessionId() {
return audioSessionId;
}
......
......@@ -673,6 +673,9 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
case MSG_SET_SKIP_SILENCE_ENABLED:
audioSink.setSkipSilenceEnabled((Boolean) message);
break;
case MSG_SET_AUDIO_SESSION_ID:
audioSink.setAudioSessionId((Integer) message);
break;
default:
super.handleMessage(messageType, message);
break;
......
......@@ -564,6 +564,9 @@ public abstract class SimpleDecoderAudioRenderer extends BaseRenderer implements
case MSG_SET_SKIP_SILENCE_ENABLED:
audioSink.setSkipSilenceEnabled((Boolean) message);
break;
case MSG_SET_AUDIO_SESSION_ID:
audioSink.setAudioSessionId((Integer) message);
break;
default:
super.handleMessage(messageType, message);
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment