Add AUDIO_BECOMING_NOISY handling to SimpleExoPlayer.

Feature enabled through the SimpleExoPlayer.setHandleAudioBecomingNoisy.

PiperOrigin-RevId: 274988924
This commit is contained in:
samrobinson 2019-10-16 10:32:20 +01:00 committed by Oliver Woodman
parent e63d3b4b61
commit 841b3a8bc7
3 changed files with 95 additions and 0 deletions

View File

@ -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)).

View File

@ -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();
}
}
}
}

View File

@ -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 <a
* href="https://developer.android.com/guide/topics/media-apps/volume-and-earphones#becoming-noisy">audio
* becoming noisy</a> 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