From de6dc85da54ffcf8fa928de3f436cfbfb1156617 Mon Sep 17 00:00:00 2001 From: bataevvlad Date: Thu, 25 Nov 2021 15:14:04 +0300 Subject: [PATCH] Improve mixWithOthers, background music volume will be lower while sound playing. --- .../java/com/zmxv/RNSound/RNSoundModule.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/zmxv/RNSound/RNSoundModule.java b/android/src/main/java/com/zmxv/RNSound/RNSoundModule.java index e6f51ec3..176840f6 100644 --- a/android/src/main/java/com/zmxv/RNSound/RNSoundModule.java +++ b/android/src/main/java/com/zmxv/RNSound/RNSoundModule.java @@ -34,11 +34,13 @@ public class RNSoundModule extends ReactContextBaseJavaModule implements AudioMa Boolean mixWithOthers = true; Double focusedPlayerKey; Boolean wasPlayingBeforeFocusChange = false; + AudioManager audioManager; public RNSoundModule(ReactApplicationContext context) { super(context); this.context = context; this.category = null; + audioManager = (AudioManager) context.getApplicationContext().getSystemService(context.AUDIO_SERVICE); } private void setOnPlay(boolean isPlaying, final Double playerKey) { @@ -204,7 +206,7 @@ protected MediaPlayer createMediaPlayer(final String fileName) { } return mediaPlayer; } - + File file = new File(fileName); if (file.exists()) { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); @@ -237,12 +239,14 @@ public void play(final Double key, final Callback callback) { // Request audio focus in Android system if (!this.mixWithOthers) { - AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); - - this.focusedPlayerKey = key; + } else { + audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); + player.setVolume(0.2f, 0.2f); } + this.focusedPlayerKey = key; + + final RNSoundModule that = this; player.setOnCompletionListener(new OnCompletionListener() { boolean callbackWasCalled = false; @@ -251,6 +255,11 @@ public void play(final Double key, final Callback callback) { public synchronized void onCompletion(MediaPlayer mp) { if (!mp.isLooping()) { setOnPlay(false, key); + + if (that.mixWithOthers && key == that.focusedPlayerKey) { + audioManager.abandonAudioFocus(that); + } + if (callbackWasCalled) return; callbackWasCalled = true; try { @@ -303,7 +312,6 @@ public void stop(final Double key, final Callback callback) { // Release audio focus in Android system if (!this.mixWithOthers && key == this.focusedPlayerKey) { - AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.abandonAudioFocus(this); } @@ -328,7 +336,6 @@ public void release(final Double key) { // Release audio focus in Android system if (!this.mixWithOthers && key == this.focusedPlayerKey) { - AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.abandonAudioFocus(this); } }