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;
    }
相关推荐
你过来啊你41 分钟前
Android性能优化之包体积优化
android
你过来啊你43 分钟前
Android性能优化之UI渲染优化
android
stringwu44 分钟前
Android开发者必备:手势冲突处理实用总结
android
你过来啊你1 小时前
Android性能优化之内存优化
android
你过来啊你1 小时前
Android性能优化之启动优化
android
apihz1 小时前
域名WHOIS信息查询免费API使用指南
android·开发语言·数据库·网络协议·tcp/ip
问道飞鱼2 小时前
【移动端知识】移动端多 WebView 互访方案:Android、iOS 与鸿蒙实现
android·ios·harmonyos·多webview互访
aningxiaoxixi2 小时前
Android 之 audiotrack
android
枷锁—sha2 小时前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
Cao_Shixin攻城狮6 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter