当前位置: 首页 > ops >正文

Android S - 重复播放按键音(上下左右、OK)

问题描述:

自定义按键音播放,在Launcher上按 上下左右和OK 按键发现会播放两次按键提示音,其他的都是正常的。

首先找一下播放的是哪个文件,在下面的README中有定义

frameworks/base/data/sounds/README.txtTouch sounds
------------effects/Effect_Tick.oggold, referenced by AudioPackage[2345].mk OriginalAudio.mkeffects/ogg/Effect_Tick.oggnew, referenced by AudioPackage[6789].mk AudioPackage7alt.mk AudioPackage10.mkeffects/ogg/Effect_Tick_48k.oggoggdec -o temp.wav ogg/Effect_Tick.oggsox temp.wav -r 48000 temp48k.wavoggenc -b 80 -o ogg/Effect_Tick_48k.ogg temp48k.waveffects/wav/Effect_Tick.wavdoes not appear to be related to the other files in any obvious way

也就是触摸和点击事件都是播放的 Effect_Tick 这个音频文件。直接检索这个在哪里调用。

 xml文件是为了替换默认声音资源,并且可以看到很多需要的如上下左右都在这里定义了,并且播放的都是Effect_Tick。

framework/base/core/res/res/xml/audio_assets.xml<!-- Mapping of UI sound effects to audio assets under /system/media/audio/ui.Modify this file to override default sound assets.
--><audio_assets version="1.0"><asset id="FX_KEY_CLICK" file="Effect_Tick.ogg"/><asset id="FX_FOCUS_NAVIGATION_UP" file="Effect_Tick.ogg"/><asset id="FX_FOCUS_NAVIGATION_DOWN" file="Effect_Tick.ogg"/><asset id="FX_FOCUS_NAVIGATION_LEFT" file="Effect_Tick.ogg"/><asset id="FX_FOCUS_NAVIGATION_RIGHT" file="Effect_Tick.ogg"/><asset id="FX_KEYPRESS_STANDARD" file="KeypressStandard.ogg"/><asset id="FX_KEYPRESS_SPACEBAR" file="KeypressSpacebar.ogg"/><asset id="FX_KEYPRESS_DELETE" file="KeypressDelete.ogg"/><asset id="FX_KEYPRESS_RETURN" file="KeypressReturn.ogg"/><asset id="FX_KEYPRESS_INVALID" file="KeypressInvalid.ogg"/><asset id="FX_BACK

再根据 SoundEffectsHelper.java 找到实现播放的方法 void playSoundEffect(int effect, int volume)

其实是在 AudioService.java 定义的 

    /** @see AudioManager#playSoundEffect(int) */public void playSoundEffect(int effectType) {playSoundEffectVolume(effectType, -1.0f);}/** @see AudioManager#playSoundEffect(int, float) */public void playSoundEffectVolume(int effectType, float volume) {// do not try to play the sound effect if the system stream is mutedif (isStreamMute(STREAM_SYSTEM)) {return;}if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) {Log.w(TAG, "AudioService effectType value " + effectType + " out of range");return;}sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE,effectType, (int) (volume * 1000), null, 0);}

 根据注释,可以确定是通过 AudioManager 调用的。目前已经确定了播放按键提示音的调用方法。再次检索。

是在这里实现的播放

    @Overridepublic void playSoundEffect(@SoundEffectConstants.SoundEffect int effectId) {checkThread();try {final AudioManager audioManager = getAudioManager();if (mFastScrollSoundEffectsEnabled&& SoundEffectConstants.isNavigationRepeat(effectId)) {audioManager.playSoundEffect(SoundEffectConstants.nextNavigationRepeatSoundEffectId());return;}if (true) {Log.e(TAG, "lichang playSoundEffect avoid click tone");return;}switch (effectId) {case SoundEffectConstants.CLICK:audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);return;case SoundEffectConstants.NAVIGATION_DOWN:case SoundEffectConstants.NAVIGATION_REPEAT_DOWN:audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);return;case SoundEffectConstants.NAVIGATION_LEFT:case SoundEffectConstants.NAVIGATION_REPEAT_LEFT:audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);return;case SoundEffectConstants.NAVIGATION_RIGHT:case SoundEffectConstants.NAVIGATION_REPEAT_RIGHT:audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);return;case SoundEffectConstants.NAVIGATION_UP:case SoundEffectConstants.NAVIGATION_REPEAT_UP:audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);return;default:throw new IllegalArgumentException("unknown effect id " + effectId +" not defined in " + SoundEffectConstants.class.getCanonicalName());}} catch (IllegalStateException e) {// Exception thrown by getAudioManager() when mView is nullLog.e(mTag, "FATAL EXCEPTION when attempting to play sound effect: " + e);e.printStackTrace();}}

直接屏蔽掉即可。或者其实从 framework/base/core/res/res/xml/audio_assets.xml 也可以看到有方向键等音频资源,可以通过检索直接定位到位置。

只在framework下检索是因为对日志进行分析,过滤Audiotrack

11:42:03.414 AudioTrack                I  start mClientAttributionSource.uid:1000 mClientAttributionSource.pid:1013  mSessionId:129 app name:system_server 
11:42:03.433 AudioTrack                I  start mClientAttributionSource.uid:1000 mClientAttributionSource.pid:1013  mSessionId:145 app name:system_server 
11:42:03.525 AudioTrack                I  stop mClientAttributionSource.uid:1000 mClientAttributionSource.pid:1013  mSessionId:129 app name:system_server 
11:42:03.553 AudioTrack                I  stop mClientAttributionSource.uid:1000 mClientAttributionSource.pid:1013  mSessionId:145 app name:system_server 
可以看到这两次声音播放都是 system_server 这个进程调用的。

补充,由于是自定义按键音播放的,因此需要实现原生的按键音开关功能,增加条件

            if (System.getInt(context.getContentResolver(), System.SOUND_EFFECTS_ENABLED, 1) == 1)
                    playEffect(context);

结束

http://www.xdnf.cn/news/13862.html

相关文章:

  • Java详解LeetCode 热题 100(32):LeetCode 138. 随机链表的复制
  • Linux常用命令加强版替代品
  • 探索弹性弦行为:从绘图到问题解决-AI云计算数值分析和代码验证
  • 永不休眠:Linux 守护进程的工作原理
  • visual studio小番茄插件某些快捷键失效
  • 1万美元iO bounty破解之旅
  • android aosp源码下编码时避免引用aidl文件飘红不自动提示的方法
  • 神经网络压缩
  • 本地windows搭建kafka
  • 青少年编程与数学 01-011 系统软件简介 17 Hadoop大数据处理框架
  • NLP进化史:从规则模板到思维链推理,七次范式革命全解析
  • Vue3 + TypeScript + Element Plus 开启边框 > 调整列宽(拖动表头)> 保存列宽(本地存储)> 加载列宽(读取本地数据)
  • 基于物品的协同过滤推荐算法实现(Java电商平台)
  • 基于用户的协同过滤推荐算法实现(Java电商平台)
  • 微服务--Gateway网关
  • 开源组件hive页面安全问题
  • 【IEEE/EI/Scopus检索】2025年第六届模式识别与数据挖掘国际会议 (PRDM 2025)
  • Python爬虫进阶:气象数据爬取中的多线程优化与异常处理技巧
  • Java并发进阶系列:深度讨论高并发跳表数据结构ConcurrentSkipListMap的源代码实现(上)
  • python类成员概要
  • 当空间与数据联动,会展中心如何打造智慧运营新范式?
  • 当机床开始“思考”,传统“制造”到“智造”升级路上的法律暗礁
  • 驱动开发前传及led驱动(s5pv210)
  • 深度学习——基于PyTorch的MNIST手写数字识别详解
  • Python数据结构与算法(6.1)——树
  • 使用 Spring Boot 和 dynamic-datasource 实现多数据源集成
  • 从 0 开始理解 Spring 的核心思想 —— IoC 和 DI(1)
  • 深入解析 SNMP Walk 的响应机制
  • 智能疲劳驾驶检测系统算法设计与研究
  • 山东大学软件学院项目实训:基于大模型的模拟面试系统项目总结(八)