Android音频焦点详解

Android通过AudioFocus机制对音频焦点进行管理。

Music通过下述代码申请Focus权限:

  1. AudioManager mAudioManager;
  2. mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);

通过AudioManager对象调用requestAudioFocus方法,有三个参数

OnAudioFocusChangeListener l,int streamType,int durationHint

1 焦点变化的监听器

private AudioManager.OnAudioFocusChangeListener mAudioFocusChange = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            switch (focusChange){
                case AudioManager.AUDIOFOCUS_LOSS:
                    //长时间丢失焦点,当其他应用申请的焦点为AUDIOFOCUS_GAIN时,
                    //会触发此回调事件,例如播放QQ音乐,网易云音乐等
                    //通常需要暂停音乐播放,若没有暂停播放就会出现和其他音乐同时输出声音
                    Log.d(TAG, "AUDIOFOCUS_LOSS"

你可能感兴趣的:(Android高级进阶)