Android 13 全局监听播放器暂停与播放 (包含系统层和第三方app)

首先设置监听:

说明:这里的代码屏蔽了系统层提示音播放暂停

java 复制代码
     AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        manager.registerAudioPlaybackCallback(new AudioPlaybackCallback() {
            @Override
            public void onPlaybackConfigChanged(List<AudioPlaybackConfiguration> configs) {
                super.onPlaybackConfigChanged(configs);
                boolean isPlayer = false;
                for (int i = 0; i < configs.size(); i++) {
                    int clientPid = configs.get(i).getClientPid();
                    int systempid = getSystemPID();
                    Log.d("播放pid:" + clientPid);
                    Log.d("系统pid:" + systempid);
                    if (systempid == clientPid) continue;
                    if (configs.get(i).isActive()) {
                        isPlayer = true;
                        break;
                    }
                }

                if (isPlayer) {
                    //开始本地播放
                    Log.d("开始本地播放");
                    updatePlayModeAnim(SWITCH_LOCAL_MODE);
                } else {
                    Log.d("停止本地播放");
                }
            }
        }, new Handler());

获取系统pid判断是否是系统层提示音播放暂停

java 复制代码
    private int systemPID = -1;

    private int getSystemPID() {
        if (systemPID >= 0) return systemPID;
        int pid = -1;
        ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> mRunningProcess = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo amProcess : mRunningProcess) {
            if (!TextUtils.isEmpty(amProcess.processName) && TextUtils.equals("system", amProcess.processName)) {
                pid = amProcess.pid;
                break;
            }
        }
        return pid;
    }
相关推荐
weixin_440784111 小时前
Java线程池工作原理浅析
android·java·开发语言·okhttp·android studio·android runtime
2501_944396191 小时前
Flutter for OpenHarmony 视力保护提醒App实战 - 性能优化技巧
android·flutter·性能优化
龚礼鹏1 小时前
图像显示框架十二——BufferQueue的工作流程(基于Android 15源码分析)
android
_F_y1 小时前
MySQL用户管理
android·mysql·adb
TheNextByte11 小时前
如何将照片从计算机传输到Android /iPhone
android·gitee·iphone
sun0077002 小时前
Android 默认的日志记录方式
android
瓦特what?2 小时前
C++编程防坑指南(小说版)
android·c++·kotlin
独处东汉3 小时前
freertos开发空气检测仪之输入子系统按键驱动测试
android·java·数据库
m0_748229993 小时前
Laravel3.x:回顾经典框架的早期特性
android
阿俊-全栈开发3 小时前
CRMEB 单商户对接汇付支付完整实现
android