欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

AudioRequster 音频焦点监听问题

程序员文章站 2022-09-06 10:17:34
关于音频焦点监听需要找到对应的播放器音频时的相关参数一一对应才能获取到焦点的回调import android.content.Context;import android.content.Intent;import android.media.AudioManager;import android.os.Build;import android.util.Log;/** * Created by Li on 2020/3/31. * 音频焦点占用 */public cl...

关于音频焦点监听需要找到对应的播放器音频时的相关参数一一对应才能获取到焦点的回调



import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;




/**
 * Created by Li on 2020/3/31.
 * 音频焦点占用
 */

public class AudioFocusManager {

        /**
         * 用AudioManager获取音频焦点避免音视频声音并发问题
         */
        private AudioManager mAudioManager;
        private AudioManager.OnAudioFocusChangeListener mAudioFocusChangeListener;

        //zxzhong 请求音频焦点 设置监听
        public int requestTheAudioFocus(final AudioListener audioListener) {
            if (Build.VERSION.SDK_INT < 8) {//Android 2.2开始(API8)才有音频焦点机制
                return 0;
            }
            if (mAudioManager == null) {
                mAudioManager = (AudioManager) AppApplication.Companion.getContext().getSystemService(Context.AUDIO_SERVICE);
            }
            if (mAudioFocusChangeListener == null) {
                mAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {//监听器
                    @Override
                    public void onAudioFocusChange(int focusChange) {
                        switch (focusChange) {
                            case AudioManager.AUDIOFOCUS_GAIN:
                            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                                //播放操作
                                audioListener.start();
                                break;

                            case AudioManager.AUDIOFOCUS_LOSS:
                            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                                //暂停操作
                                audioListener.pause();
                                break;
                            default:
                                break;
                        }
                    }
                };
            }

            // type:   STREAM_RING   AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
           // 参数一/二/三  都必须跟播放音频时 一 一 对应
           //主要是这里 *************************************************
            int requestFocusResult = mAudioManager.requestAudioFocus(mAudioFocusChangeListener,
                    AudioManager.STREAM_MUSIC,//AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
                    AudioManager.AUDIOFOCUS_GAIN);//AUDIOFOCUS_GAIN_TRANSIENT
             

            
            Log.w("audioFocus","  requestFocusResult = "+requestFocusResult+"  AUDIOFOCUS "+AudioManager.AUDIOFOCUS_REQUEST_GRANTED);


            return requestFocusResult;
        }

        //zxzhong 暂停、播放完成或退到后台释放音频焦点
        public void releaseTheAudioFocus() {
            if (mAudioManager != null && mAudioFocusChangeListener != null) {
                mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);
            }
        }


        public interface AudioListener{
            void start();
            void pause();
        }


    public void pauseMusic(Context context) {//暂停
        Intent freshIntent = new Intent();
        freshIntent.setAction("com.android.music.musicservicecommand.pause");
        freshIntent.putExtra("command", "pause");
        context.sendBroadcast(freshIntent);
    }


    public void playCurrentMusic(Context context){//继续播放
        Intent freshIntent = new Intent();
        freshIntent.setAction("com.android.music.musicservicecommand");
        freshIntent.putExtra("command", "togglepause");
        context.sendBroadcast(freshIntent);
    }






}

本文地址:https://blog.csdn.net/qq_32216895/article/details/107471787

相关标签: Android音视频开发