mirror of
https://github.com/androidx/media.git
synced 2025-05-07 15:40:37 +08:00
Handle stream volume register/unregister errors
Issue: #8106 Issue: #8087 PiperOrigin-RevId: 338664455
This commit is contained in:
parent
a184924322
commit
160ee9d890
@ -18,6 +18,9 @@
|
|||||||
([#5887](https://github.com/google/ExoPlayer/issues/5887)).
|
([#5887](https://github.com/google/ExoPlayer/issues/5887)).
|
||||||
* Fix bug where `AnalyticsListener` callbacks can arrive in the wrong
|
* Fix bug where `AnalyticsListener` callbacks can arrive in the wrong
|
||||||
order ([#8048](https://github.com/google/ExoPlayer/issues/8048)).
|
order ([#8048](https://github.com/google/ExoPlayer/issues/8048)).
|
||||||
|
* Suppress exceptions from registering/unregistering the stream volume
|
||||||
|
receiver ([#8087](https://github.com/google/ExoPlayer/issues/8087)),
|
||||||
|
([#8106](https://github.com/google/ExoPlayer/issues/8106)).
|
||||||
* Track selection:
|
* Track selection:
|
||||||
* Add option to specify multiple preferred audio or text languages.
|
* Add option to specify multiple preferred audio or text languages.
|
||||||
* UI:
|
* UI:
|
||||||
|
@ -21,7 +21,9 @@ import android.content.Intent;
|
|||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
|
import com.google.android.exoplayer2.util.Log;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
|
||||||
/** A manager that wraps {@link AudioManager} to control/listen audio stream volume. */
|
/** A manager that wraps {@link AudioManager} to control/listen audio stream volume. */
|
||||||
@ -37,6 +39,8 @@ import com.google.android.exoplayer2.util.Util;
|
|||||||
void onStreamVolumeChanged(int streamVolume, boolean streamMuted);
|
void onStreamVolumeChanged(int streamVolume, boolean streamMuted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String TAG = "StreamVolumeManager";
|
||||||
|
|
||||||
// TODO(b/151280453): Replace the hidden intent action with an official one.
|
// TODO(b/151280453): Replace the hidden intent action with an official one.
|
||||||
// Copied from AudioManager#VOLUME_CHANGED_ACTION
|
// Copied from AudioManager#VOLUME_CHANGED_ACTION
|
||||||
private static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
|
private static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
|
||||||
@ -48,12 +52,11 @@ import com.google.android.exoplayer2.util.Util;
|
|||||||
private final Handler eventHandler;
|
private final Handler eventHandler;
|
||||||
private final Listener listener;
|
private final Listener listener;
|
||||||
private final AudioManager audioManager;
|
private final AudioManager audioManager;
|
||||||
private final VolumeChangeReceiver receiver;
|
|
||||||
|
|
||||||
|
@Nullable private VolumeChangeReceiver receiver;
|
||||||
@C.StreamType private int streamType;
|
@C.StreamType private int streamType;
|
||||||
private int volume;
|
private int volume;
|
||||||
private boolean muted;
|
private boolean muted;
|
||||||
private boolean released;
|
|
||||||
|
|
||||||
/** Creates a manager. */
|
/** Creates a manager. */
|
||||||
public StreamVolumeManager(Context context, Handler eventHandler, Listener listener) {
|
public StreamVolumeManager(Context context, Handler eventHandler, Listener listener) {
|
||||||
@ -68,9 +71,14 @@ import com.google.android.exoplayer2.util.Util;
|
|||||||
volume = getVolumeFromManager(audioManager, streamType);
|
volume = getVolumeFromManager(audioManager, streamType);
|
||||||
muted = getMutedFromManager(audioManager, streamType);
|
muted = getMutedFromManager(audioManager, streamType);
|
||||||
|
|
||||||
receiver = new VolumeChangeReceiver();
|
VolumeChangeReceiver receiver = new VolumeChangeReceiver();
|
||||||
IntentFilter filter = new IntentFilter(VOLUME_CHANGED_ACTION);
|
IntentFilter filter = new IntentFilter(VOLUME_CHANGED_ACTION);
|
||||||
applicationContext.registerReceiver(receiver, filter);
|
try {
|
||||||
|
applicationContext.registerReceiver(receiver, filter);
|
||||||
|
this.receiver = receiver;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
Log.w(TAG, "Error registering stream volume receiver", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the audio stream type. */
|
/** Sets the audio stream type. */
|
||||||
@ -159,11 +167,14 @@ import com.google.android.exoplayer2.util.Util;
|
|||||||
|
|
||||||
/** 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() {
|
||||||
if (released) {
|
if (receiver != null) {
|
||||||
return;
|
try {
|
||||||
|
applicationContext.unregisterReceiver(receiver);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
Log.w(TAG, "Error unregistering stream volume receiver", e);
|
||||||
|
}
|
||||||
|
receiver = null;
|
||||||
}
|
}
|
||||||
applicationContext.unregisterReceiver(receiver);
|
|
||||||
released = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateVolumeAndNotifyIfChanged() {
|
private void updateVolumeAndNotifyIfChanged() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user