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;
    }
相关推荐
灿烂阳光g4 小时前
domain_auto_trans,source_domain,untrusted_app
android·linux
低调小一6 小时前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
雨白6 小时前
Java 多线程指南:从基础用法到线程安全
android·java
00后程序员张7 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
程序员江同学8 小时前
ovCompose + AI 开发跨三端的 Now in Kotlin App
android·kotlin·harmonyos
2501_915106328 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
消失的旧时光-19439 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
灿烂阳光g10 小时前
SELinux 策略文件编写
android·linux
.豆鲨包10 小时前
【Android】Viewpager2实现无限轮播图
android·java