Skip to content

Commit

Permalink
Add AudioComponent to the Player interface
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrewlewis authored and ojw28 committed Jun 22, 2018
1 parent 8802da6 commit ddda4a0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ public void setSessionAvailabilityListener(SessionAvailabilityListener listener)

// Player implementation.

@Override
public AudioComponent getAudioComponent() {
return null;
}

@Override
public VideoComponent getVideoComponent() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public void handleMessage(Message msg) {
internalPlayerHandler = new Handler(internalPlayer.getPlaybackLooper());
}

@Override
public AudioComponent getAudioComponent() {
return null;
}

@Override
public VideoComponent getVideoComponent() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
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;
Expand All @@ -50,6 +52,44 @@
*/
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 {

Expand Down Expand Up @@ -428,6 +468,10 @@ public void onTimelineChanged(Timeline timeline, Object manifest) {
*/
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
* 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
Expand All @@ -83,8 +84,7 @@ public interface VideoListener extends com.google.android.exoplayer2.video.Video

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;
Expand Down Expand Up @@ -219,6 +219,11 @@ protected SimpleExoPlayer(
}
}

@Override
public AudioComponent getAudioComponent() {
return this;
}

@Override
public VideoComponent getVideoComponent() {
return this;
Expand Down Expand Up @@ -345,6 +350,45 @@ public void clearVideoTextureView(TextureView textureView) {
}
}

@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>
Expand Down Expand Up @@ -399,63 +443,6 @@ public void removeAnalyticsListener(AnalyticsListener listener) {
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.
*
Expand Down Expand Up @@ -489,13 +476,6 @@ public Format getAudioFormat() {
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
public abstract class StubExoPlayer implements ExoPlayer {

@Override
public AudioComponent getAudioComponent() {
throw new UnsupportedOperationException();
}

@Override
public VideoComponent getVideoComponent() {
throw new UnsupportedOperationException();
Expand Down

0 comments on commit ddda4a0

Please sign in to comment.