Skip to content

Commit

Permalink
Handle stream volume register/unregister errors
Browse files Browse the repository at this point in the history
Issue: #8106
Issue: #8087
PiperOrigin-RevId: 338664455
  • Loading branch information
andrewlewis authored and ojw28 committed Nov 2, 2020
1 parent a184924 commit 160ee9d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
([#5887](https://github.com/google/ExoPlayer/issues/5887)).
* Fix bug where `AnalyticsListener` callbacks can arrive in the wrong
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:
* Add option to specify multiple preferred audio or text languages.
* UI:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Handler;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;

/** A manager that wraps {@link AudioManager} to control/listen audio stream volume. */
Expand All @@ -37,6 +39,8 @@ public interface Listener {
void onStreamVolumeChanged(int streamVolume, boolean streamMuted);
}

private static final String TAG = "StreamVolumeManager";

// TODO(b/151280453): Replace the hidden intent action with an official one.
// Copied from AudioManager#VOLUME_CHANGED_ACTION
private static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
Expand All @@ -48,12 +52,11 @@ public interface Listener {
private final Handler eventHandler;
private final Listener listener;
private final AudioManager audioManager;
private final VolumeChangeReceiver receiver;

@Nullable private VolumeChangeReceiver receiver;
@C.StreamType private int streamType;
private int volume;
private boolean muted;
private boolean released;

/** Creates a manager. */
public StreamVolumeManager(Context context, Handler eventHandler, Listener listener) {
Expand All @@ -68,9 +71,14 @@ public StreamVolumeManager(Context context, Handler eventHandler, Listener liste
volume = getVolumeFromManager(audioManager, streamType);
muted = getMutedFromManager(audioManager, streamType);

receiver = new VolumeChangeReceiver();
VolumeChangeReceiver receiver = new VolumeChangeReceiver();
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. */
Expand Down Expand Up @@ -159,11 +167,14 @@ public void setMuted(boolean muted) {

/** Releases the manager. It must be called when the manager is no longer required. */
public void release() {
if (released) {
return;
if (receiver != null) {
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() {
Expand Down

0 comments on commit 160ee9d

Please sign in to comment.