Skip to content

Commit eb11028

Browse files
fafamaninfil00p
authored andcommitted
Request audio focus when playing; Pause audio when audio focus is lost; resume playing when audio focus is granted again.
This closes #88
1 parent cc4a6d3 commit eb11028

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

src/android/AudioHandler.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2727
import android.content.Context;
2828
import android.content.pm.PackageManager;
2929
import android.media.AudioManager;
30+
import android.media.AudioManager.OnAudioFocusChangeListener;
3031
import android.net.Uri;
3132
import android.os.Build;
3233
import android.util.Log;
@@ -55,8 +56,9 @@ Licensed to the Apache Software Foundation (ASF) under one
5556
public class AudioHandler extends CordovaPlugin {
5657

5758
public static String TAG = "AudioHandler";
58-
HashMap<String, AudioPlayer> players; // Audio player object
59-
ArrayList<AudioPlayer> pausedForPhone; // Audio players that were paused when phone call came in
59+
HashMap<String, AudioPlayer> players; // Audio player object
60+
ArrayList<AudioPlayer> pausedForPhone; // Audio players that were paused when phone call came in
61+
ArrayList<AudioPlayer> pausedForFocus; // Audio players that were paused when focus was lost
6062
private int origVolumeStream = -1;
6163
private CallbackContext messageChannel;
6264

@@ -76,6 +78,7 @@ public class AudioHandler extends CordovaPlugin {
7678
public AudioHandler() {
7779
this.players = new HashMap<String, AudioPlayer>();
7880
this.pausedForPhone = new ArrayList<AudioPlayer>();
81+
this.pausedForFocus = new ArrayList<AudioPlayer>();
7982
}
8083

8184

@@ -297,6 +300,7 @@ public void stopRecordingAudio(String id) {
297300
public void startPlayingAudio(String id, String file) {
298301
AudioPlayer audio = getOrCreatePlayer(id, file);
299302
audio.startPlaying(file);
303+
getAudioFocus();
300304
}
301305

302306
/**
@@ -376,6 +380,55 @@ else if (output == 1) {
376380
}
377381
}
378382

383+
public void pauseAllLostFocus() {
384+
for (AudioPlayer audio : this.players.values()) {
385+
if (audio.getState() == AudioPlayer.STATE.MEDIA_RUNNING.ordinal()) {
386+
this.pausedForFocus.add(audio);
387+
audio.pausePlaying();
388+
}
389+
}
390+
}
391+
392+
public void resumeAllGainedFocus() {
393+
for (AudioPlayer audio : this.pausedForFocus) {
394+
audio.startPlaying(null);
395+
}
396+
this.pausedForFocus.clear();
397+
}
398+
399+
/**
400+
* Get the the audio focus
401+
*/
402+
private OnAudioFocusChangeListener focusChangeListener = new OnAudioFocusChangeListener() {
403+
public void onAudioFocusChange(int focusChange) {
404+
switch (focusChange) {
405+
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
406+
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
407+
case (AudioManager.AUDIOFOCUS_LOSS) :
408+
pauseAllLostFocus();
409+
break;
410+
case (AudioManager.AUDIOFOCUS_GAIN):
411+
resumeAllGainedFocus();
412+
break;
413+
default:
414+
break;
415+
}
416+
}
417+
};
418+
419+
public void getAudioFocus() {
420+
AudioManager am = (AudioManager) this.cordova.getActivity().getSystemService(Context.AUDIO_SERVICE);
421+
int result = am.requestAudioFocus(focusChangeListener,
422+
AudioManager.STREAM_MUSIC,
423+
AudioManager.AUDIOFOCUS_GAIN);
424+
425+
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
426+
System.out.println("AudioHandler.getAudioFocus() Error: Got " + result + " instead of " + AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
427+
}
428+
429+
}
430+
431+
379432
/**
380433
* Get the audio device to be used for playback.
381434
*

0 commit comments

Comments
 (0)