其他分享
首页 > 其他分享> > 实测Android音频的焦点获取和归还

实测Android音频的焦点获取和归还

作者:互联网

实测Android音频的焦点获取和归还

在这里插入图片描述

前言

最近老板想在产品中的短视频后者直播播放的时候对于手机中的音乐播放器进行暂停播放,并且退出视频播放后手机的音乐播放器还能继续播放之前的音乐。

先试试微信,emmm,确实可以。

Android官网:管理音频焦点

官网管理音频焦点准则:

不同版本音频焦点的处理方式不太相同:

API介绍

处理音频焦点都是通过AudioManager这个类,如下是获得该类实例的方法:
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

requestAudioFocus()//用于申请音频焦点
abandonAudioFocus() //用于释放音频焦点
AudioManager.OnAudioFocusChangeListener 接口,提供了 onAudioFocusChange() 方法来监听音频焦点变化

new AudioManager.OnAudioFocusChangeListener() {
  @Override
  public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
      case AudioManager.AUDIOFOCUS_GAIN:
        // TBD 继续播放
        break;
      case AudioManager.AUDIOFOCUS_LOSS:
        // TBD 停止播放
        break;
      case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        // TBD 暂停播放
        break;
      case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // TBD 混音播放 
        break;
      default:
        break;
    }
  }
};

实测代码:

核心是需要把 AudioManager.AUDIOFOCUS_GAIN 改为 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT

public class MainActivity extends Activity {

    private AudioManager mAudioManager;
    private AudioFocusRequest mFocusRequest;
    private AudioManager.OnAudioFocusChangeListener mListener;
    private AudioAttributes mAttribute;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mListener = new AudioManager.OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                switch (focusChange) {
                    case AudioManager.AUDIOFOCUS_GAIN:
                       // TBD 继续播放
                        break;
                    case AudioManager.AUDIOFOCUS_LOSS:
                       // TBD 停止播放
                        break;
                    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                        // TBD 暂停播放
                        break;
                    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                        // TBD 混音播放 
                        break;
                    default:
                        break;
                }

            }
        };
        //android 版本 5.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mAttribute = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .build();
        }
        //android 版本 8.0
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
                    .setWillPauseWhenDucked(true)
                    .setAcceptsDelayedFocusGain(true)
                    .setOnAudioFocusChangeListener(mListener, mHandler)
                    .setAudioAttributes(mAttribute)
                    .build();
        }
    }
    private void requestAudioFocus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
					mAudioManager.requestAudioFocus(mFocusRequest);
        } else {
          mAudioManager.requestAudioFocus(mListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        }
    }
    private void abandonAudioFocus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mAudioManager.abandonAudioFocusRequest(mFocusRequest);
        } else {
          mAudioManager.abandonAudioFocus(mListener);
        }

    }
}

参考:

https://segmentfault.com/a/1190000022234509

https://www.jianshu.com/p/26ea60c499a7

文章到这里就全部讲述完啦,若有其他需要交流的可以留言哦~!~!

想阅读作者的更多文章,可以查看我 个人博客 和公共号:
振兴书城

标签:AudioManager,焦点,音频,AUDIOFOCUS,实测,GAIN,Android,播放
来源: https://blog.csdn.net/stven_king/article/details/122835280