From 9d92bd2b789c6b5de77800a52e5dde04ad5ef28d Mon Sep 17 00:00:00 2001 From: lucasrocali Date: Mon, 9 Dec 2019 19:46:39 -0300 Subject: [PATCH] feat: handle Audio onFocusChanged if paused (#308) * feat: priority high to expand notification * fix: bug AUDIOFOCUS_GAIN playing if paused * feat: handle AUDIOFOCUS_LOSS_TRANSIENT if isPlaying() * Update package.json Co-Authored-By: Tanguy Antoine --- .../react/MusicControlAudioFocusListener.java | 14 ++++++++++++-- .../tanguyantoine/react/MusicControlModule.java | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/tanguyantoine/react/MusicControlAudioFocusListener.java b/android/src/main/java/com/tanguyantoine/react/MusicControlAudioFocusListener.java index ba0bc89b..064416b9 100644 --- a/android/src/main/java/com/tanguyantoine/react/MusicControlAudioFocusListener.java +++ b/android/src/main/java/com/tanguyantoine/react/MusicControlAudioFocusListener.java @@ -14,6 +14,8 @@ public class MusicControlAudioFocusListener implements AudioManager.OnAudioFocus private AudioManager mAudioManager; private AudioFocusRequest mFocusRequest; + private boolean mPlayOnAudioFocus = false; + MusicControlAudioFocusListener(ReactApplicationContext context, MusicControlEventEmitter emitter, MusicControlVolumeListener volume) { this.emitter = emitter; @@ -25,16 +27,24 @@ public class MusicControlAudioFocusListener implements AudioManager.OnAudioFocus @Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { + abandonAudioFocus(); + mPlayOnAudioFocus = false; emitter.onStop(); } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { - emitter.onPause(); + if (MusicControlModule.INSTANCE.isPlaying()) { + mPlayOnAudioFocus = true; + emitter.onPause(); + } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { volume.setCurrentVolume(40); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (volume.getCurrentVolume() != 100) { volume.setCurrentVolume(100); } - emitter.onPlay(); + if (mPlayOnAudioFocus) { + emitter.onPlay(); + } + mPlayOnAudioFocus = false; } } diff --git a/android/src/main/java/com/tanguyantoine/react/MusicControlModule.java b/android/src/main/java/com/tanguyantoine/react/MusicControlModule.java index 5fdd550b..7f7c0233 100644 --- a/android/src/main/java/com/tanguyantoine/react/MusicControlModule.java +++ b/android/src/main/java/com/tanguyantoine/react/MusicControlModule.java @@ -171,6 +171,7 @@ public void init() { } nb = new NotificationCompat.Builder(context, CHANNEL_ID); nb.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); + nb.setPriority(NotificationCompat.PRIORITY_HIGH); updateNotificationMediaStyle(); @@ -556,4 +557,8 @@ public enum NotificationClose { PAUSED, NEVER } + + public boolean isPlaying() { + return isPlaying; + } }