I would like to know how do I implement the behaviour, so that my radio streaming would stop playing if other audio would come in to play - for instance, user starts playing music, makes a call etc.
In other words, how does my ExoPlayers knows that other audio is being played, or if it doesn't, do I have to listen for mediaplayer and call events?
Here is an example i made for an audio state manager, you can use it as a template. (created in 2016)
As per the question - You need to use an OnAudioFocusChangeListener
, Read the comments in the example:
public class AudioStateManager {
private Context mContext;
private AudioStateListener mListener;
private AudioManager mAudioManager;
private AudioFocusChangeListener mAudioFocusChangeListener;
private AudioBecomingNoisyReceiver mAudioBecomingNoisyReceiver;
public interface AudioStateListener {
void onAudioFocusRequested();
void onAudioFocusGained();
void onVolumeLoweringRequested();
}
public void AudioStateManager(Context context) {
this.mContext = context;
mAudioManager = (AudioManager)
context.getSystemService(Context.AUDIO_SERVICE);
mAudioFocusChangeListener = new AudioFocusChangeListener();
mAudioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver();
}
public void registerListener(AudioStateListener listener) {
this.mListener = listener;
}
/**
* Call when about to play
*
* @return true if audio focus is granted.
*/
public boolean requestAudioFocus() {
int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
/**
* Call when no longer in app or no longer need audioFocus
*/
public void abandonAudioFocus() {
mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);
}
/**
* Call when playing media
*/
public void registerNoiseReceiver() {
IntentFilter filter = new
IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
mContext.registerReceiver(mAudioBecomingNoisyReceiver, filter);
}
/**
* Call when media is paused/stopped
*/
public void unregisterNoiseReceiver() {
mContext.unregisterReceiver(mAudioBecomingNoisyReceiver);
}
private class AudioFocusChangeListener
implements OnAudioFocusChangeListener {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
// another app gained audio focus
// STOP PLAYBACK
dispatchAudioFocusRequested();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// another app want you to pause media
// PAUSE
dispatchAudioFocusRequested();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// LOWER VOLUME (but keep playing)
dispatchVolumeLoweringRequested();
break;
case AudioManager.AUDIOFOCUS_GAIN:
// START PLAYBACK
dispatchAudioFocusGained();
break;
}
}
}
private void dispatchAudioFocusRequested() {
if (mListener != null)
mListener.onAudioFocusRequested();
}
private void dispatchAudioFocusGained() {
if (mListener != null)
mListener.onAudioFocusGained();
}
private void dispatchVolumeLoweringRequested() {
if (mListener != null)
mListener.onVolumeLoweringRequested();
}
/**
* Called when unplugging your headphones for example
* Register when start playing music
*/
private class AudioBecomingNoisyReceiver
extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Pause the music
Log.i("TAG", "Audio became noisy - Pause Music");
}
}
}
You need to read about Audio Focus.
来源:https://stackoverflow.com/questions/49593139/stop-audio-playing-when-another-apps-audio-comes-in