Stop audio playing when another app's audio comes in

≡放荡痞女 提交于 2019-12-06 10:28:44

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!