Add possibility to set the audio session id

Issue: #6975
PiperOrigin-RevId: 299328798
This commit is contained in:
kimvde 2020-03-06 12:44:36 +00:00 committed by Oliver Woodman
parent ab21f710bb
commit c982f4c4a0
6 changed files with 42 additions and 0 deletions

View File

@ -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

View File

@ -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();

View File

@ -131,6 +131,12 @@ public interface Renderer extends PlayerMessage.Target {
* telling whether to enable or disable skipping silences in the audio stream.
*/
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.

View File

@ -672,6 +672,21 @@ public class SimpleExoPlayer extends BasePlayer
return audioAttributes;
}
@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;

View File

@ -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;

View File

@ -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;