Skip to content

Commit 4a02c5f

Browse files
oceanjulesrohitjoins
authored andcommitted
Extend Player interface, overloading 4 device-volume methods with flags
Previously, ExoPlayerImpl had volume flags hardcoded to SHOW_UI, but now the developer can choose what happens on volume change. The old methods have been deprecated. PiperOrigin-RevId: 523974358
1 parent e7727f4 commit 4a02c5f

File tree

13 files changed

+512
-116
lines changed

13 files changed

+512
-116
lines changed

extensions/cast/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
apply from: "$gradle.ext.exoplayerSettingsDir/common_library_config.gradle"
1515

1616
dependencies {
17-
api 'com.google.android.gms:play-services-cast-framework:21.2.0'
17+
api 'com.google.android.gms:play-services-cast-framework:21.3.0'
1818
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
1919
implementation project(modulePrefix + 'library-common')
2020
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion

extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,22 +753,50 @@ public boolean isDeviceMuted() {
753753
return false;
754754
}
755755

756-
/** This method is not supported and does nothing. */
756+
/**
757+
* @deprecated Use {@link #setDeviceVolume(int, int)} instead.
758+
*/
759+
@Deprecated
757760
@Override
758761
public void setDeviceVolume(int volume) {}
759762

760763
/** This method is not supported and does nothing. */
761764
@Override
765+
public void setDeviceVolume(int volume, @C.VolumeFlags int flags) {}
766+
767+
/**
768+
* @deprecated Use {@link #increaseDeviceVolume(int)} instead.
769+
*/
770+
@Deprecated
771+
@Override
762772
public void increaseDeviceVolume() {}
763773

764774
/** This method is not supported and does nothing. */
765775
@Override
776+
public void increaseDeviceVolume(@C.VolumeFlags int flags) {}
777+
778+
/**
779+
* @deprecated Use {@link #decreaseDeviceVolume(int)} instead.
780+
*/
781+
@Deprecated
782+
@Override
766783
public void decreaseDeviceVolume() {}
767784

768785
/** This method is not supported and does nothing. */
769786
@Override
787+
public void decreaseDeviceVolume(@C.VolumeFlags int flags) {}
788+
789+
/**
790+
* @deprecated Use {@link #setDeviceMuted(boolean, int)} instead.
791+
*/
792+
@Deprecated
793+
@Override
770794
public void setDeviceMuted(boolean muted) {}
771795

796+
/** This method is not supported and does nothing. */
797+
@Override
798+
public void setDeviceMuted(boolean muted, @C.VolumeFlags int flags) {}
799+
772800
// Internal methods.
773801

774802
// Call deprecated callbacks.

library/common/src/main/java/com/google/android/exoplayer2/C.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,37 @@ private C() {}
404404
/** The default stream type used by audio renderers. Equal to {@link #STREAM_TYPE_MUSIC}. */
405405
public static final int STREAM_TYPE_DEFAULT = STREAM_TYPE_MUSIC;
406406

407+
/**
408+
* Volume flags to be used when setting or adjusting device volume. The value can be either 0 or a
409+
* combination of the following flags: {@link #VOLUME_FLAG_SHOW_UI}, {@link
410+
* #VOLUME_FLAG_ALLOW_RINGER_MODES}, {@link #VOLUME_FLAG_PLAY_SOUND}, {@link
411+
* #VOLUME_FLAG_REMOVE_SOUND_AND_VIBRATE}, {@link #VOLUME_FLAG_VIBRATE}.
412+
*/
413+
@Documented
414+
@Retention(RetentionPolicy.SOURCE)
415+
@Target({TYPE_USE})
416+
@IntDef(
417+
flag = true,
418+
value = {
419+
VOLUME_FLAG_SHOW_UI,
420+
VOLUME_FLAG_ALLOW_RINGER_MODES,
421+
VOLUME_FLAG_PLAY_SOUND,
422+
VOLUME_FLAG_REMOVE_SOUND_AND_VIBRATE,
423+
VOLUME_FLAG_VIBRATE,
424+
})
425+
public @interface VolumeFlags {}
426+
/** See {@link AudioManager#FLAG_SHOW_UI}. */
427+
public static final int VOLUME_FLAG_SHOW_UI = AudioManager.FLAG_SHOW_UI;
428+
/** See {@link AudioManager#FLAG_ALLOW_RINGER_MODES}. */
429+
public static final int VOLUME_FLAG_ALLOW_RINGER_MODES = AudioManager.FLAG_ALLOW_RINGER_MODES;
430+
/** See {@link AudioManager#FLAG_PLAY_SOUND}. */
431+
public static final int VOLUME_FLAG_PLAY_SOUND = AudioManager.FLAG_PLAY_SOUND;
432+
/** See {@link AudioManager#FLAG_REMOVE_SOUND_AND_VIBRATE}. */
433+
public static final int VOLUME_FLAG_REMOVE_SOUND_AND_VIBRATE =
434+
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE;
435+
/** See {@link AudioManager#FLAG_VIBRATE}. */
436+
public static final int VOLUME_FLAG_VIBRATE = AudioManager.FLAG_VIBRATE;
437+
407438
/**
408439
* Content types for audio attributes. One of:
409440
*

library/common/src/main/java/com/google/android/exoplayer2/ForwardingPlayer.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,30 +848,66 @@ public boolean isDeviceMuted() {
848848
return player.isDeviceMuted();
849849
}
850850

851-
/** Calls {@link Player#setDeviceVolume(int)} on the delegate. */
851+
/**
852+
* @deprecated Use {@link #setDeviceVolume(int, int)} instead.
853+
*/
854+
@Deprecated
852855
@Override
853856
public void setDeviceVolume(int volume) {
854857
player.setDeviceVolume(volume);
855858
}
856859

857-
/** Calls {@link Player#increaseDeviceVolume()} on the delegate. */
860+
/** Calls {@link Player#setDeviceVolume(int, int)} on the delegate. */
861+
@Override
862+
public void setDeviceVolume(int volume, @C.VolumeFlags int flags) {
863+
player.setDeviceVolume(volume, flags);
864+
}
865+
866+
/**
867+
* @deprecated Use {@link #increaseDeviceVolume(int)} instead.
868+
*/
869+
@Deprecated
858870
@Override
859871
public void increaseDeviceVolume() {
860872
player.increaseDeviceVolume();
861873
}
862874

863-
/** Calls {@link Player#decreaseDeviceVolume()} on the delegate. */
875+
/** Calls {@link Player#increaseDeviceVolume(int)} on the delegate. */
876+
@Override
877+
public void increaseDeviceVolume(@C.VolumeFlags int flags) {
878+
player.increaseDeviceVolume(flags);
879+
}
880+
881+
/**
882+
* @deprecated Use {@link #decreaseDeviceVolume(int)} instead.
883+
*/
884+
@Deprecated
864885
@Override
865886
public void decreaseDeviceVolume() {
866887
player.decreaseDeviceVolume();
867888
}
868889

869-
/** Calls {@link Player#setDeviceMuted(boolean)} on the delegate. */
890+
/** Calls {@link Player#decreaseDeviceVolume(int)} on the delegate. */
891+
@Override
892+
public void decreaseDeviceVolume(@C.VolumeFlags int flags) {
893+
player.decreaseDeviceVolume(flags);
894+
}
895+
896+
/**
897+
* @deprecated Use {@link #setDeviceMuted(boolean, int)} instead.
898+
*/
899+
@Deprecated
870900
@Override
871901
public void setDeviceMuted(boolean muted) {
872902
player.setDeviceMuted(muted);
873903
}
874904

905+
/** Calls {@link Player#setDeviceMuted(boolean, int)} on the delegate. */
906+
@Override
907+
public void setDeviceMuted(boolean muted, @C.VolumeFlags int flags) {
908+
player.setDeviceMuted(muted, flags);
909+
}
910+
875911
/** Returns the {@link Player} to which operations are forwarded. */
876912
public Player getWrappedPlayer() {
877913
return player;

library/common/src/main/java/com/google/android/exoplayer2/Player.java

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ public static final class Builder {
378378
COMMAND_GET_DEVICE_VOLUME,
379379
COMMAND_SET_VOLUME,
380380
COMMAND_SET_DEVICE_VOLUME,
381+
COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS,
381382
COMMAND_ADJUST_DEVICE_VOLUME,
383+
COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS,
382384
COMMAND_SET_VIDEO_SURFACE,
383385
COMMAND_GET_TEXT,
384386
COMMAND_SET_TRACK_SELECTION_PARAMETERS,
@@ -1426,7 +1428,9 @@ default void onMetadata(Metadata metadata) {}
14261428
* <li>{@link #COMMAND_GET_DEVICE_VOLUME}
14271429
* <li>{@link #COMMAND_SET_VOLUME}
14281430
* <li>{@link #COMMAND_SET_DEVICE_VOLUME}
1431+
* <li>{@link #COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS}
14291432
* <li>{@link #COMMAND_ADJUST_DEVICE_VOLUME}
1433+
* <li>{@link #COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS}
14301434
* <li>{@link #COMMAND_SET_VIDEO_SURFACE}
14311435
* <li>{@link #COMMAND_GET_TEXT}
14321436
* <li>{@link #COMMAND_SET_TRACK_SELECTION_PARAMETERS}
@@ -1470,7 +1474,9 @@ default void onMetadata(Metadata metadata) {}
14701474
COMMAND_GET_DEVICE_VOLUME,
14711475
COMMAND_SET_VOLUME,
14721476
COMMAND_SET_DEVICE_VOLUME,
1477+
COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS,
14731478
COMMAND_ADJUST_DEVICE_VOLUME,
1479+
COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS,
14741480
COMMAND_SET_VIDEO_SURFACE,
14751481
COMMAND_GET_TEXT,
14761482
COMMAND_SET_TRACK_SELECTION_PARAMETERS,
@@ -1776,27 +1782,36 @@ default void onMetadata(Metadata metadata) {}
17761782
* #isCommandAvailable(int) available}.
17771783
*/
17781784
int COMMAND_SET_VOLUME = 24;
1785+
1786+
/**
1787+
* @deprecated Use {@link #COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS} instead.
1788+
*/
1789+
@Deprecated int COMMAND_SET_DEVICE_VOLUME = 25;
17791790
/**
1780-
* Command to set the device volume.
1791+
* Command to set the device volume with volume flags.
17811792
*
1782-
* <p>The {@link #setDeviceVolume(int)} method must only be called if this command is {@linkplain
1783-
* #isCommandAvailable(int) available}.
1793+
* <p>The {@link #setDeviceVolume(int, int)} method must only be called if this command is
1794+
* {@linkplain #isCommandAvailable(int) available}.
17841795
*/
1785-
int COMMAND_SET_DEVICE_VOLUME = 25;
1796+
int COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS = 33;
17861797

17871798
/**
1788-
* Command to increase and decrease the device volume and mute it.
1799+
* @deprecated Use {@link #COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS} instead.
1800+
*/
1801+
@Deprecated int COMMAND_ADJUST_DEVICE_VOLUME = 26;
1802+
/**
1803+
* Command to increase and decrease the device volume and mute it with volume flags.
17891804
*
17901805
* <p>The following methods must only be called if this command is {@linkplain
17911806
* #isCommandAvailable(int) available}:
17921807
*
17931808
* <ul>
1794-
* <li>{@link #increaseDeviceVolume()}
1795-
* <li>{@link #decreaseDeviceVolume()}
1796-
* <li>{@link #setDeviceMuted(boolean)}
1809+
* <li>{@link #increaseDeviceVolume(int)}
1810+
* <li>{@link #decreaseDeviceVolume(int)}
1811+
* <li>{@link #setDeviceMuted(boolean, int)}
17971812
* </ul>
17981813
*/
1799-
int COMMAND_ADJUST_DEVICE_VOLUME = 26;
1814+
int COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS = 34;
18001815

18011816
/**
18021817
* Command to set and clear the surface on which to render the video.
@@ -3067,36 +3082,68 @@ default void onMetadata(Metadata metadata) {}
30673082
boolean isDeviceMuted();
30683083

30693084
/**
3070-
* Sets the volume of the device.
3085+
* @deprecated Use {@link #setDeviceVolume(int, int)} instead.
3086+
*/
3087+
@Deprecated
3088+
void setDeviceVolume(@IntRange(from = 0) int volume);
3089+
3090+
/**
3091+
* Sets the volume of the device with volume flags.
30713092
*
3072-
* <p>This method must only be called if {@link #COMMAND_SET_DEVICE_VOLUME} is {@linkplain
3073-
* #getAvailableCommands() available}.
3093+
* <p>This method must only be called if {@link #COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS} is
3094+
* {@linkplain #getAvailableCommands() available}.
30743095
*
30753096
* @param volume The volume to set.
3097+
* @param flags Either 0 or a bitwise combination of one or more {@link C.VolumeFlags}.
30763098
*/
3077-
void setDeviceVolume(@IntRange(from = 0) int volume);
3099+
void setDeviceVolume(@IntRange(from = 0) int volume, int flags);
3100+
3101+
/**
3102+
* @deprecated Use {@link #increaseDeviceVolume(int)} instead.
3103+
*/
3104+
@Deprecated
3105+
void increaseDeviceVolume();
30783106

30793107
/**
30803108
* Increases the volume of the device.
30813109
*
3082-
* <p>This method must only be called if {@link #COMMAND_ADJUST_DEVICE_VOLUME} is {@linkplain
3083-
* #getAvailableCommands() available}.
3110+
* <p>This method must only be called if {@link #COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS} is
3111+
* {@linkplain #getAvailableCommands() available}.
3112+
*
3113+
* @param flags Either 0 or a bitwise combination of one or more {@link C.VolumeFlags}.
30843114
*/
3085-
void increaseDeviceVolume();
3115+
void increaseDeviceVolume(@C.VolumeFlags int flags);
3116+
3117+
/**
3118+
* @deprecated Use {@link #decreaseDeviceVolume(int)} instead.
3119+
*/
3120+
@Deprecated
3121+
void decreaseDeviceVolume();
30863122

30873123
/**
30883124
* Decreases the volume of the device.
30893125
*
3090-
* <p>This method must only be called if {@link #COMMAND_ADJUST_DEVICE_VOLUME} is {@linkplain
3091-
* #getAvailableCommands() available}.
3126+
* <p>This method must only be called if {@link #COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS} is
3127+
* {@linkplain #getAvailableCommands() available}.
3128+
*
3129+
* @param flags Either 0 or a bitwise combination of one or more {@link C.VolumeFlags}.
30923130
*/
3093-
void decreaseDeviceVolume();
3131+
void decreaseDeviceVolume(@C.VolumeFlags int flags);
3132+
3133+
/**
3134+
* @deprecated Use {@link #setDeviceMuted(boolean, int)} instead.
3135+
*/
3136+
@Deprecated
3137+
void setDeviceMuted(boolean muted);
30943138

30953139
/**
30963140
* Sets the mute state of the device.
30973141
*
3098-
* <p>This method must only be called if {@link #COMMAND_ADJUST_DEVICE_VOLUME} is {@linkplain
3099-
* #getAvailableCommands() available}.
3142+
* <p>This method must only be called if {@link #COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS} is
3143+
* {@linkplain #getAvailableCommands() available}.
3144+
*
3145+
* @param muted Whether to set the device to be muted or not
3146+
* @param flags Either 0 or a bitwise combination of one or more {@link C.VolumeFlags}.
31003147
*/
3101-
void setDeviceMuted(boolean muted);
3148+
void setDeviceMuted(boolean muted, @C.VolumeFlags int flags);
31023149
}

0 commit comments

Comments
 (0)