From ddda4a026dd882b44b079c8d7434967799431982 Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Fri, 22 Jun 2018 03:49:56 -0700 Subject: [PATCH] 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 --- .../exoplayer2/ext/cast/CastPlayer.java | 5 + .../android/exoplayer2/ExoPlayerImpl.java | 5 + .../com/google/android/exoplayer2/Player.java | 44 +++++++ .../android/exoplayer2/SimpleExoPlayer.java | 114 ++++++++---------- .../exoplayer2/testutil/StubExoPlayer.java | 5 + 5 files changed, 106 insertions(+), 67 deletions(-) diff --git a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java index 8c0d4b88c0..21e853dd62 100644 --- a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java +++ b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java @@ -283,6 +283,11 @@ public final class CastPlayer implements Player { // Player implementation. + @Override + public AudioComponent getAudioComponent() { + return null; + } + @Override public VideoComponent getVideoComponent() { return null; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java index e803fb30ee..9a224aca5d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java @@ -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; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/Player.java b/library/core/src/main/java/com/google/android/exoplayer2/Player.java index 995691d163..aa053ae147 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/Player.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/Player.java @@ -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. + * + *

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. + * + *

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. + * + *

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(); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java b/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java index 0fa7279079..7ca801da6b 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java @@ -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. *

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

- * 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. - *

- * 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. - *

- * 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. */ diff --git a/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java b/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java index 214c2b6a5e..0718c5dd26 100644 --- a/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java +++ b/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java @@ -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();