diff --git a/RELEASENOTES.md b/RELEASENOTES.md index fc0b0bb088..b3efe1ae55 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -84,6 +84,8 @@ `C.MSG_SET_OUTPUT_BUFFER_RENDERER`. * Use `VideoDecoderRenderer` as an implementation of `VideoDecoderOutputBufferRenderer`, instead of `VideoDecoderSurfaceView`. +* Add automatic audio becoming noisy handling to `SimpleExoPlayer`, + available through `SimpleExoPlayer.setHandleAudioBecomingNoisy`. * Add `Timeline.Window.isLive` to indicate that a window is a live stream ([#2668](https://github.com/google/ExoPlayer/issues/2668) and [#5973](https://github.com/google/ExoPlayer/issues/5973)). diff --git a/library/core/src/main/java/com/google/android/exoplayer2/AudioBecomingNoisyManager.java b/library/core/src/main/java/com/google/android/exoplayer2/AudioBecomingNoisyManager.java new file mode 100644 index 0000000000..667ef9c290 --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/AudioBecomingNoisyManager.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.media.AudioManager; + +/* package */ final class AudioBecomingNoisyManager { + + private final Context context; + private final AudioBecomingNoisyReceiver receiver; + private boolean receiverRegistered; + + public interface EventListener { + void onAudioBecomingNoisy(); + } + + public AudioBecomingNoisyManager(Context context, EventListener listener) { + this.context = context.getApplicationContext(); + this.receiver = new AudioBecomingNoisyReceiver(listener); + } + + /** + * Enables the {@link AudioBecomingNoisyManager} which calls {@link + * EventListener#onAudioBecomingNoisy()} upon receiving an intent of {@link + * AudioManager#ACTION_AUDIO_BECOMING_NOISY}. + * + * @param enabled True if the listener should be notified when audio is becoming noisy. + */ + public void setEnabled(boolean enabled) { + if (enabled && !receiverRegistered) { + context.registerReceiver( + receiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); + receiverRegistered = true; + } else if (!enabled && receiverRegistered) { + context.unregisterReceiver(receiver); + receiverRegistered = false; + } + } + + private static final class AudioBecomingNoisyReceiver extends BroadcastReceiver { + private final EventListener listener; + + public AudioBecomingNoisyReceiver(EventListener listener) { + this.listener = listener; + } + + @Override + public void onReceive(Context context, Intent intent) { + if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) { + listener.onAudioBecomingNoisy(); + } + } + } +} 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 9e59448c2d..1f7802ac72 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 @@ -322,6 +322,7 @@ public class SimpleExoPlayer extends BasePlayer private final BandwidthMeter bandwidthMeter; private final AnalyticsCollector analyticsCollector; + private final AudioBecomingNoisyManager audioBecomingNoisyManager; private final AudioFocusManager audioFocusManager; private final WakeLockManager wakeLockManager; @@ -453,6 +454,7 @@ public class SimpleExoPlayer extends BasePlayer if (drmSessionManager instanceof DefaultDrmSessionManager) { ((DefaultDrmSessionManager) drmSessionManager).addListener(eventHandler, analyticsCollector); } + audioBecomingNoisyManager = new AudioBecomingNoisyManager(context, componentListener); audioFocusManager = new AudioFocusManager(context, componentListener); wakeLockManager = new WakeLockManager(context); } @@ -765,6 +767,18 @@ public class SimpleExoPlayer extends BasePlayer analyticsCollector.removeListener(listener); } + /** + * Sets whether the player should pause automatically when audio is rerouted from a headset to + * device speakers. See the audio + * becoming noisy documentation for more information. + * + * @param handleAudioBecomingNoisy True if the player should handle audio becoming noisy. + */ + public void setHandleAudioBecomingNoisy(boolean handleAudioBecomingNoisy) { + audioBecomingNoisyManager.setEnabled(handleAudioBecomingNoisy); + } + /** * Sets a {@link PriorityTaskManager}, or null to clear a previously set priority task manager. * @@ -1476,6 +1490,7 @@ public class SimpleExoPlayer extends BasePlayer SurfaceHolder.Callback, TextureView.SurfaceTextureListener, AudioFocusManager.PlayerControl, + AudioBecomingNoisyManager.EventListener, Player.EventListener { // VideoRendererEventListener implementation @@ -1687,6 +1702,13 @@ public class SimpleExoPlayer extends BasePlayer updatePlayWhenReady(getPlayWhenReady(), playerCommand); } + // AudioBecomingNoisyManager.EventListener implementation. + + @Override + public void onAudioBecomingNoisy() { + setPlayWhenReady(false); + } + // Player.EventListener implementation. @Override