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;
    }
相关推荐
星霜笔记3 小时前
GitMob — 手机端 GitHub 管理工具
android·kotlin·github·android jetpack
LiuYaoheng3 小时前
问题记录:Android Studio Low memory
android·ide·android studio
独隅4 小时前
Python 标准库 (Standard Library) 全面使用指南
android·开发语言·python
always_TT4 小时前
strlen、strcpy、strcat等常用字符串函数
android
qqty12174 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
2401_895521344 小时前
MySQL中between and的基本用法
android·数据库·mysql
云云鬼才5 小时前
CoCo编辑器、图形化编程怎么调用Scheme(跳转应用)
android
Jason__Young7 小时前
Android ViewModel为什么能够跨越Activity的生命周期?
android
TechMix7 小时前
【性能优化】RenderThread各工作阶段梳理
android·性能优化
草莓熊Lotso8 小时前
MySQL 内置函数指南:日期、字符串、数学函数实战
android·java·linux·运维·数据库·c++·mysql