Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
feat: handle Audio onFocusChanged if paused (#308)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
lucasrocali and tanguyantoine committed Dec 9, 2019
1 parent 9be009c commit 9d92bd2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -556,4 +557,8 @@ public enum NotificationClose {
PAUSED,
NEVER
}

public boolean isPlaying() {
return isPlaying;
}
}

0 comments on commit 9d92bd2

Please sign in to comment.