Move OnAudioFocusChangeListener to own class
This commit is contained in:
parent
3bd032b720
commit
493b620e63
@ -119,7 +119,6 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
Player.Listener,
|
Player.Listener,
|
||||||
BandwidthMeter.EventListener,
|
BandwidthMeter.EventListener,
|
||||||
BecomingNoisyListener,
|
BecomingNoisyListener,
|
||||||
AudioManager.OnAudioFocusChangeListener,
|
|
||||||
DrmSessionEventListener,
|
DrmSessionEventListener,
|
||||||
AdEvent.AdEventListener {
|
AdEvent.AdEventListener {
|
||||||
|
|
||||||
@ -215,6 +214,7 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
private final ThemedReactContext themedReactContext;
|
private final ThemedReactContext themedReactContext;
|
||||||
private final AudioManager audioManager;
|
private final AudioManager audioManager;
|
||||||
private final AudioBecomingNoisyReceiver audioBecomingNoisyReceiver;
|
private final AudioBecomingNoisyReceiver audioBecomingNoisyReceiver;
|
||||||
|
private final AudioManager.OnAudioFocusChangeListener audioFocusChangeListener;
|
||||||
|
|
||||||
// store last progress event values to avoid sending unnecessary messages
|
// store last progress event values to avoid sending unnecessary messages
|
||||||
private long lastPos = -1;
|
private long lastPos = -1;
|
||||||
@ -270,6 +270,7 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||||
themedReactContext.addLifecycleEventListener(this);
|
themedReactContext.addLifecycleEventListener(this);
|
||||||
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
|
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
|
||||||
|
audioFocusChangeListener = new OnAudioFocusChangedListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPlayingAd() {
|
private boolean isPlayingAd() {
|
||||||
@ -903,11 +904,54 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
bandwidthMeter.removeEventListener(this);
|
bandwidthMeter.removeEventListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class OnAudioFocusChangedListener implements AudioManager.OnAudioFocusChangeListener {
|
||||||
|
private final ReactExoplayerView view;
|
||||||
|
|
||||||
|
private OnAudioFocusChangedListener(ReactExoplayerView view) {
|
||||||
|
this.view = view;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAudioFocusChange(int focusChange) {
|
||||||
|
switch (focusChange) {
|
||||||
|
case AudioManager.AUDIOFOCUS_LOSS:
|
||||||
|
view.hasAudioFocus = false;
|
||||||
|
view.eventEmitter.audioFocusChanged(false);
|
||||||
|
view.pausePlayback();
|
||||||
|
view.audioManager.abandonAudioFocus(this);
|
||||||
|
break;
|
||||||
|
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
|
||||||
|
view.eventEmitter.audioFocusChanged(false);
|
||||||
|
break;
|
||||||
|
case AudioManager.AUDIOFOCUS_GAIN:
|
||||||
|
view.hasAudioFocus = true;
|
||||||
|
view.eventEmitter.audioFocusChanged(true);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (view.player != null) {
|
||||||
|
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
|
||||||
|
// Lower the volume
|
||||||
|
if (!view.muted) {
|
||||||
|
view.player.setVolume(view.audioVolume * 0.8f);
|
||||||
|
}
|
||||||
|
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
|
||||||
|
// Raise it back to normal
|
||||||
|
if (!view.muted) {
|
||||||
|
view.player.setVolume(view.audioVolume * 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean requestAudioFocus() {
|
private boolean requestAudioFocus() {
|
||||||
if (disableFocus || srcUri == null || this.hasAudioFocus) {
|
if (disableFocus || srcUri == null || this.hasAudioFocus) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
int result = audioManager.requestAudioFocus(this,
|
int result = audioManager.requestAudioFocus(audioFocusChangeListener,
|
||||||
AudioManager.STREAM_MUSIC,
|
AudioManager.STREAM_MUSIC,
|
||||||
AudioManager.AUDIOFOCUS_GAIN);
|
AudioManager.AUDIOFOCUS_GAIN);
|
||||||
return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
|
return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
|
||||||
@ -973,7 +1017,7 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
if (isFullscreen) {
|
if (isFullscreen) {
|
||||||
setFullscreen(false);
|
setFullscreen(false);
|
||||||
}
|
}
|
||||||
audioManager.abandonAudioFocus(this);
|
audioManager.abandonAudioFocus(audioFocusChangeListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateResumePosition() {
|
private void updateResumePosition() {
|
||||||
@ -1011,43 +1055,6 @@ class ReactExoplayerView extends FrameLayout implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// AudioManager.OnAudioFocusChangeListener implementation
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAudioFocusChange(int focusChange) {
|
|
||||||
switch (focusChange) {
|
|
||||||
case AudioManager.AUDIOFOCUS_LOSS:
|
|
||||||
this.hasAudioFocus = false;
|
|
||||||
eventEmitter.audioFocusChanged(false);
|
|
||||||
pausePlayback();
|
|
||||||
audioManager.abandonAudioFocus(this);
|
|
||||||
break;
|
|
||||||
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
|
|
||||||
eventEmitter.audioFocusChanged(false);
|
|
||||||
break;
|
|
||||||
case AudioManager.AUDIOFOCUS_GAIN:
|
|
||||||
this.hasAudioFocus = true;
|
|
||||||
eventEmitter.audioFocusChanged(true);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (player != null) {
|
|
||||||
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
|
|
||||||
// Lower the volume
|
|
||||||
if (!muted) {
|
|
||||||
player.setVolume(audioVolume * 0.8f);
|
|
||||||
}
|
|
||||||
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
|
|
||||||
// Raise it back to normal
|
|
||||||
if (!muted) {
|
|
||||||
player.setVolume(audioVolume * 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AudioBecomingNoisyListener implementation
|
// AudioBecomingNoisyListener implementation
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user