Add AudioComponent to the Player interface

This will make it possible for ImaAdsLoader to access the player volume when
used with SimpleExoPlayer.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201664189
This commit is contained in:
andrewlewis 2018-06-22 03:49:56 -07:00 committed by Oliver Woodman
parent 8802da6bb3
commit ddda4a026d
5 changed files with 106 additions and 67 deletions

View file

@ -283,6 +283,11 @@ public final class CastPlayer implements Player {
// Player implementation.
@Override
public AudioComponent getAudioComponent() {
return null;
}
@Override
public VideoComponent getVideoComponent() {
return null;

View file

@ -137,6 +137,11 @@ import java.util.concurrent.CopyOnWriteArraySet;
internalPlayerHandler = new Handler(internalPlayer.getPlaybackLooper());
}
@Override
public AudioComponent getAudioComponent() {
return null;
}
@Override
public VideoComponent getVideoComponent() {
return null;

View file

@ -22,9 +22,11 @@ import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import com.google.android.exoplayer2.audio.AudioAttributes;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.text.TextOutput;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoListener;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -50,6 +52,44 @@ import java.lang.annotation.RetentionPolicy;
*/
public interface Player {
/** The audio component of a {@link Player}. */
interface AudioComponent {
/**
* Sets the attributes for audio playback, used by the underlying audio track. If not set, the
* default audio attributes will be used. They are suitable for general media playback.
*
* <p>Setting the audio attributes during playback may introduce a short gap in audio output as
* the audio track is recreated. A new audio session id will also be generated.
*
* <p>If tunneling is enabled by the track selector, the specified audio attributes will be
* ignored, but they will take effect if audio is later played without tunneling.
*
* <p>If the device is running a build before platform API version 21, audio attributes cannot
* be set directly on the underlying audio track. In this case, the usage will be mapped onto an
* equivalent stream type using {@link Util#getStreamTypeForAudioUsage(int)}.
*
* @param audioAttributes The attributes to use for audio playback.
*/
void setAudioAttributes(AudioAttributes audioAttributes);
/** Returns the attributes for audio playback. */
AudioAttributes getAudioAttributes();
/** Returns the audio session identifier, or {@link C#AUDIO_SESSION_ID_UNSET} if not set. */
int getAudioSessionId();
/**
* Sets the audio volume, with 0 being silence and 1 being unity gain.
*
* @param audioVolume The audio volume.
*/
void setVolume(float audioVolume);
/** Returns the audio volume, with 0 being silence and 1 being unity gain. */
float getVolume();
}
/** The video component of a {@link Player}. */
interface VideoComponent {
@ -428,6 +468,10 @@ public interface Player {
*/
int TIMELINE_CHANGE_REASON_DYNAMIC = 2;
/** Returns the component of this player for audio output, or null if audio is not supported. */
@Nullable
AudioComponent getAudioComponent();
/** Returns the component of this player for video output, or null if video is not supported. */
@Nullable
VideoComponent getVideoComponent();

View file

@ -57,7 +57,8 @@ import java.util.concurrent.CopyOnWriteArraySet;
* be obtained from {@link ExoPlayerFactory}.
*/
@TargetApi(16)
public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player.TextComponent {
public class SimpleExoPlayer
implements ExoPlayer, Player.AudioComponent, Player.VideoComponent, Player.TextComponent {
/** @deprecated Use {@link com.google.android.exoplayer2.video.VideoListener}. */
@Deprecated
@ -83,8 +84,7 @@ public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player
private Surface surface;
private boolean ownsSurface;
@C.VideoScalingMode
private int videoScalingMode;
private @C.VideoScalingMode int videoScalingMode;
private SurfaceHolder surfaceHolder;
private TextureView textureView;
private int surfaceWidth;
@ -219,6 +219,11 @@ public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player
}
}
@Override
public AudioComponent getAudioComponent() {
return this;
}
@Override
public VideoComponent getVideoComponent() {
return this;
@ -345,6 +350,45 @@ public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player
}
}
@Override
public void setAudioAttributes(AudioAttributes audioAttributes) {
this.audioAttributes = audioAttributes;
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) {
player
.createMessage(renderer)
.setType(C.MSG_SET_AUDIO_ATTRIBUTES)
.setPayload(audioAttributes)
.send();
}
}
}
@Override
public AudioAttributes getAudioAttributes() {
return audioAttributes;
}
@Override
public int getAudioSessionId() {
return audioSessionId;
}
@Override
public void setVolume(float audioVolume) {
this.audioVolume = audioVolume;
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) {
player.createMessage(renderer).setType(C.MSG_SET_VOLUME).setPayload(audioVolume).send();
}
}
}
@Override
public float getVolume() {
return audioVolume;
}
/**
* Sets the stream type for audio playback, used by the underlying audio track.
* <p>
@ -399,63 +443,6 @@ public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player
analyticsCollector.removeListener(listener);
}
/**
* Sets the attributes for audio playback, used by the underlying audio track. If not set, the
* default audio attributes will be used. They are suitable for general media playback.
* <p>
* Setting the audio attributes during playback may introduce a short gap in audio output as the
* audio track is recreated. A new audio session id will also be generated.
* <p>
* If tunneling is enabled by the track selector, the specified audio attributes will be ignored,
* but they will take effect if audio is later played without tunneling.
* <p>
* If the device is running a build before platform API version 21, audio attributes cannot be set
* directly on the underlying audio track. In this case, the usage will be mapped onto an
* equivalent stream type using {@link Util#getStreamTypeForAudioUsage(int)}.
*
* @param audioAttributes The attributes to use for audio playback.
*/
public void setAudioAttributes(AudioAttributes audioAttributes) {
this.audioAttributes = audioAttributes;
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) {
player
.createMessage(renderer)
.setType(C.MSG_SET_AUDIO_ATTRIBUTES)
.setPayload(audioAttributes)
.send();
}
}
}
/**
* Returns the attributes for audio playback.
*/
public AudioAttributes getAudioAttributes() {
return audioAttributes;
}
/**
* Sets the audio volume, with 0 being silence and 1 being unity gain.
*
* @param audioVolume The audio volume.
*/
public void setVolume(float audioVolume) {
this.audioVolume = audioVolume;
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) {
player.createMessage(renderer).setType(C.MSG_SET_VOLUME).setPayload(audioVolume).send();
}
}
}
/**
* Returns the audio volume, with 0 being silence and 1 being unity gain.
*/
public float getVolume() {
return audioVolume;
}
/**
* Sets the {@link PlaybackParams} governing audio playback.
*
@ -489,13 +476,6 @@ public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player
return audioFormat;
}
/**
* Returns the audio session identifier, or {@link C#AUDIO_SESSION_ID_UNSET} if not set.
*/
public int getAudioSessionId() {
return audioSessionId;
}
/**
* Returns {@link DecoderCounters} for video, or null if no video is being played.
*/

View file

@ -34,6 +34,11 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
*/
public abstract class StubExoPlayer implements ExoPlayer {
@Override
public AudioComponent getAudioComponent() {
throw new UnsupportedOperationException();
}
@Override
public VideoComponent getVideoComponent() {
throw new UnsupportedOperationException();