Commit e250900a by gyumin Committed by Ian Baker

Add mute/unmute to DeviceComponent

PiperOrigin-RevId: 305648273
parent d33c5ac0
...@@ -198,6 +198,30 @@ public class StreamVolumeManagerTest { ...@@ -198,6 +198,30 @@ public class StreamVolumeManagerTest {
} }
@Test @Test
public void setVolumeMuted_changesMuteState() {
testThread.runOnMainThread(
() -> {
int minVolume = streamVolumeManager.getMinVolume();
int maxVolume = streamVolumeManager.getMaxVolume();
if (minVolume == maxVolume || minVolume > 0) {
return;
}
streamVolumeManager.setVolume(maxVolume);
assertThat(streamVolumeManager.isMuted()).isFalse();
streamVolumeManager.setMuted(true);
assertThat(streamVolumeManager.isMuted()).isTrue();
assertThat(testListener.lastStreamVolumeMuted).isTrue();
streamVolumeManager.setMuted(false);
assertThat(streamVolumeManager.isMuted()).isFalse();
assertThat(testListener.lastStreamVolumeMuted).isFalse();
assertThat(testListener.lastStreamVolume).isEqualTo(maxVolume);
});
}
@Test
public void setStreamType_notifiesStreamTypeAndVolume() { public void setStreamType_notifiesStreamTypeAndVolume() {
testThread.runOnMainThread( testThread.runOnMainThread(
() -> { () -> {
...@@ -250,6 +274,7 @@ public class StreamVolumeManagerTest { ...@@ -250,6 +274,7 @@ public class StreamVolumeManagerTest {
@C.StreamType private int lastStreamType; @C.StreamType private int lastStreamType;
private int lastStreamVolume; private int lastStreamVolume;
private boolean lastStreamVolumeMuted;
public final CountDownLatch onStreamVolumeChangedLatch; public final CountDownLatch onStreamVolumeChangedLatch;
public TestListener() { public TestListener() {
...@@ -262,8 +287,9 @@ public class StreamVolumeManagerTest { ...@@ -262,8 +287,9 @@ public class StreamVolumeManagerTest {
} }
@Override @Override
public void onStreamVolumeChanged(int streamVolume) { public void onStreamVolumeChanged(int streamVolume, boolean streamMuted) {
lastStreamVolume = streamVolume; lastStreamVolume = streamVolume;
lastStreamVolumeMuted = streamMuted;
onStreamVolumeChangedLatch.countDown(); onStreamVolumeChangedLatch.countDown();
} }
} }
......
...@@ -397,6 +397,9 @@ public interface Player { ...@@ -397,6 +397,9 @@ public interface Player {
*/ */
int getDeviceVolume(); int getDeviceVolume();
/** Gets whether the device is muted or not. */
boolean isDeviceMuted();
/** /**
* Sets the volume of the device. * Sets the volume of the device.
* *
...@@ -409,6 +412,9 @@ public interface Player { ...@@ -409,6 +412,9 @@ public interface Player {
/** Decreases the volume of the device. */ /** Decreases the volume of the device. */
void decreaseDeviceVolume(); void decreaseDeviceVolume();
/** Sets the mute state of the device. */
void setDeviceMuted(boolean muted);
} }
/** /**
......
...@@ -1776,6 +1776,12 @@ public class SimpleExoPlayer extends BasePlayer ...@@ -1776,6 +1776,12 @@ public class SimpleExoPlayer extends BasePlayer
} }
@Override @Override
public boolean isDeviceMuted() {
verifyApplicationThread();
return streamVolumeManager.isMuted();
}
@Override
public void setDeviceVolume(int volume) { public void setDeviceVolume(int volume) {
verifyApplicationThread(); verifyApplicationThread();
streamVolumeManager.setVolume(volume); streamVolumeManager.setVolume(volume);
...@@ -1793,6 +1799,12 @@ public class SimpleExoPlayer extends BasePlayer ...@@ -1793,6 +1799,12 @@ public class SimpleExoPlayer extends BasePlayer
streamVolumeManager.decreaseVolume(); streamVolumeManager.decreaseVolume();
} }
@Override
public void setDeviceMuted(boolean muted) {
verifyApplicationThread();
streamVolumeManager.setMuted(muted);
}
// Internal methods. // Internal methods.
private void removeSurfaceCallbacks() { private void removeSurfaceCallbacks() {
...@@ -2217,9 +2229,9 @@ public class SimpleExoPlayer extends BasePlayer ...@@ -2217,9 +2229,9 @@ public class SimpleExoPlayer extends BasePlayer
} }
@Override @Override
public void onStreamVolumeChanged(int streamVolume) { public void onStreamVolumeChanged(int streamVolume, boolean streamMuted) {
for (DeviceListener deviceListener : deviceListeners) { for (DeviceListener deviceListener : deviceListeners) {
deviceListener.onDeviceVolumeChanged(streamVolume); deviceListener.onDeviceVolumeChanged(streamVolume, streamMuted);
} }
} }
......
...@@ -33,8 +33,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -33,8 +33,8 @@ import com.google.android.exoplayer2.util.Util;
/** Called when the audio stream type is changed. */ /** Called when the audio stream type is changed. */
void onStreamTypeChanged(@C.StreamType int streamType); void onStreamTypeChanged(@C.StreamType int streamType);
/** Called when the audio stream volume is changed. */ /** Called when the audio stream volume or mute state is changed. */
void onStreamVolumeChanged(int streamVolume); void onStreamVolumeChanged(int streamVolume, boolean streamMuted);
} }
// TODO(b/151280453): Replace the hidden intent action with an official one. // TODO(b/151280453): Replace the hidden intent action with an official one.
...@@ -52,6 +52,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -52,6 +52,7 @@ import com.google.android.exoplayer2.util.Util;
@C.StreamType private int streamType; @C.StreamType private int streamType;
private int volume; private int volume;
private boolean muted;
/** Creates a manager. */ /** Creates a manager. */
public StreamVolumeManager(Context context, Handler eventHandler, Listener listener) { public StreamVolumeManager(Context context, Handler eventHandler, Listener listener) {
...@@ -63,7 +64,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -63,7 +64,8 @@ import com.google.android.exoplayer2.util.Util;
(AudioManager) applicationContext.getSystemService(Context.AUDIO_SERVICE)); (AudioManager) applicationContext.getSystemService(Context.AUDIO_SERVICE));
streamType = C.STREAM_TYPE_DEFAULT; streamType = C.STREAM_TYPE_DEFAULT;
volume = audioManager.getStreamVolume(streamType); volume = getVolumeFromManager(audioManager, streamType);
muted = getMutedFromManager(audioManager, streamType);
receiver = new VolumeChangeReceiver(); receiver = new VolumeChangeReceiver();
IntentFilter filter = new IntentFilter(VOLUME_CHANGED_ACTION); IntentFilter filter = new IntentFilter(VOLUME_CHANGED_ACTION);
...@@ -102,6 +104,11 @@ import com.google.android.exoplayer2.util.Util; ...@@ -102,6 +104,11 @@ import com.google.android.exoplayer2.util.Util;
return volume; return volume;
} }
/** Gets whether the current audio stream is muted or not. */
public boolean isMuted() {
return muted;
}
/** /**
* Sets the volume with the given value for the current audio stream. The value should be between * Sets the volume with the given value for the current audio stream. The value should be between
* {@link #getMinVolume()} and {@link #getMaxVolume()}, otherwise it will be ignored. * {@link #getMinVolume()} and {@link #getMaxVolume()}, otherwise it will be ignored.
...@@ -138,16 +145,42 @@ import com.google.android.exoplayer2.util.Util; ...@@ -138,16 +145,42 @@ import com.google.android.exoplayer2.util.Util;
updateVolumeAndNotifyIfChanged(); updateVolumeAndNotifyIfChanged();
} }
/** Sets the mute state of the current audio stream. */
public void setMuted(boolean muted) {
if (Util.SDK_INT >= 23) {
audioManager.adjustStreamVolume(
streamType, muted ? AudioManager.ADJUST_MUTE : AudioManager.ADJUST_UNMUTE, VOLUME_FLAGS);
} else {
audioManager.setStreamMute(streamType, muted);
}
updateVolumeAndNotifyIfChanged();
}
/** Releases the manager. It must be called when the manager is no longer required. */ /** Releases the manager. It must be called when the manager is no longer required. */
public void release() { public void release() {
applicationContext.unregisterReceiver(receiver); applicationContext.unregisterReceiver(receiver);
} }
private void updateVolumeAndNotifyIfChanged() { private void updateVolumeAndNotifyIfChanged() {
int newVolume = audioManager.getStreamVolume(streamType); int newVolume = getVolumeFromManager(audioManager, streamType);
if (volume != newVolume) { boolean newMuted = getMutedFromManager(audioManager, streamType);
if (volume != newVolume || muted != newMuted) {
volume = newVolume; volume = newVolume;
listener.onStreamVolumeChanged(newVolume); muted = newMuted;
listener.onStreamVolumeChanged(newVolume, newMuted);
}
}
private static int getVolumeFromManager(AudioManager audioManager, @C.StreamType int streamType) {
return audioManager.getStreamVolume(streamType);
}
private static boolean getMutedFromManager(
AudioManager audioManager, @C.StreamType int streamType) {
if (Util.SDK_INT >= 23) {
return audioManager.isStreamMute(streamType);
} else {
return audioManager.getStreamVolume(streamType) == 0;
} }
} }
......
...@@ -23,6 +23,6 @@ public interface DeviceListener { ...@@ -23,6 +23,6 @@ public interface DeviceListener {
/** Called when the device information changes. */ /** Called when the device information changes. */
default void onDeviceInfoChanged(DeviceInfo deviceInfo) {} default void onDeviceInfoChanged(DeviceInfo deviceInfo) {}
/** Called when the device volume changes. */ /** Called when the device volume or mute state changes. */
default void onDeviceVolumeChanged(int volume) {} default void onDeviceVolumeChanged(int volume, boolean muted) {}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment