Android 通过ADB方式获取&设置音量
创始人
2024-11-04 13:38:49
0

在Android 开发中,会涉及到各种音量调整环节,可以通过Demo的方式实时修改体现也可以通ADB的方式来实现.简单记录一下.

一 获取设备音频全部信息

通过dumpsys 指令获取audio信息,即可获得设备全部信息.

dumpsys audio
二 Android的音量类型(举例:Android 8.1)
    /* These values must be kept in sync with system/audio.h */     /*      * If these are modified, please also update Settings.System.VOLUME_SETTINGS      * and attrs.xml and AudioManager.java.      */     /** Used to identify the default audio stream volume */     public static final int STREAM_DEFAULT = -1;     /** Used to identify the volume of audio streams for phone calls */     public static final int STREAM_VOICE_CALL = 0;     /** Used to identify the volume of audio streams for system sounds */     public static final int STREAM_SYSTEM = 1;     /** Used to identify the volume of audio streams for the phone ring and message alerts */     public static final int STREAM_RING = 2;     /** Used to identify the volume of audio streams for music playback */     public static final int STREAM_MUSIC = 3;     /** Used to identify the volume of audio streams for alarms */     public static final int STREAM_ALARM = 4;     /** Used to identify the volume of audio streams for notifications */     public static final int STREAM_NOTIFICATION = 5;     /** Used to identify the volume of audio streams for phone calls when connected on bluetooth */     public static final int STREAM_BLUETOOTH_SCO = 6;     /** Used to identify the volume of audio streams for enforced system sounds in certain      * countries (e.g camera in Japan) */     public static final int STREAM_SYSTEM_ENFORCED = 7;     /** Used to identify the volume of audio streams for DTMF tones */     public static final int STREAM_DTMF = 8;     /** Used to identify the volume of audio streams exclusively transmitted through the      *  speaker (TTS) of the device */     public static final int STREAM_TTS = 9;     /** Used to identify the volume of audio streams for accessibility prompts */     public static final int STREAM_ACCESSIBILITY = 10;

 Android 12 有新增

/** @hide Used to identify the volume of audio streams for virtual assistant */     public static final int STREAM_ASSISTANT = 11;

 常用的有以下五种:

  • 获取设备的电话音量 - STREAM_VOICE_CALL 
  • 获取设备的系统音量 - STREAM_SYSTEM
  • 获取设备的铃音音量 - STREAM_RING
  • 获取设备的音乐音量 - STREAM_MUSIC
  • 获取设备的闹钟音量- STREAM_ALARM
举例:设置多媒体音量大小
adb shell media volume --show --stream 3 --get   输出:     [v] will control stream=3 (STREAM_MUSIC)     [v] will get volume     [v] Connecting to AudioService     [v] volume is 0 in range [0..15]  # 注:即当前STREAM_MUSIC是0,最小0,最大15
举例:设置多媒体音量大小
adb shell media volume --show --stream 3 --set 10   输出:     [v] will control stream=3 (STREAM_MUSIC)     [v] will set volume to index=10     [v] Connecting to AudioService   # 注:设置系统音量为0(stream=1)后,设备会进入静音模式,之后再修改其他模式音量会失败
Android 11 以上获取/设置方式

较高版本获取/设置有所变更, 官方文档如下:

In Android 11 and Android 12, media utility no longer exists. That's why you got the error since your shell could not find the utility in locations mentioned in its PATH. That utility's commands have been moved to a service name MediaSessionService. To access those commands invoke this service using cmd command.

具体的操作案例演示如下: 

// 必须先建立连接 adb shell cmd media_session   C:\adb>adb shell cmd media_session  volume  --stream 3 --get [V] will control stream=3 (STREAM_MUSIC) [V] will get volume [V] Connecting to AudioService [V] volume is 5 in range [0..15]   C:\adb>adb shell cmd media_session  volume --show --stream 3 --set 11 [V] will control stream=3 (STREAM_MUSIC) [V] will set volume to index=11 [V] Connecting to AudioService

 

相关内容

热门资讯

据文件显示!aapoker发牌... 据文件显示!aapoker发牌逻辑,湖北休闲辅助,方针教程(有挂攻略)1、这是跨平台的湖北休闲辅助轻...
第1分钟辅助!大菠萝挂机辅助(... 第1分钟辅助!大菠萝挂机辅助(辅助挂)真是真的是有辅助技巧(有挂教学)亲,关键说明,大菠萝挂机辅助透...
据悉!wepoker-h5下载... 据悉!wepoker-h5下载,微信小程序微乐辅助器下载,妙计教程(真是有挂)1、微信小程序微乐辅助...
五分钟辅助!丽水都莱脚本辅助视... 五分钟辅助!丽水都莱脚本辅助视频(辅助挂)其实真的有辅助方法(有人有挂)在进入丽水都莱脚本辅助视频软...
有了最新消息!wepoker有... 有了最新消息!wepoker有什么规律,天天填大坑辅助器免费下载,学习教程(确实有挂)1、让任何用户...
四分钟辅助!九游辅助真的假的(... 四分钟辅助!九游辅助真的假的(辅助挂)其实真的有辅助插件(了解有挂)一、九游辅助真的假的游戏安装教程...
随着!hhpoker是真的还是... 随着!hhpoker是真的还是假的,小程序牵手跑得有辅助器,要领教程(有挂方式)1、打开软件启动之后...
4分钟辅助!极速鱼虾蟹辅助(辅... 4分钟辅助!极速鱼虾蟹辅助(辅助挂)真是存在有辅助教程(有挂方略)1、极速鱼虾蟹辅助辅助软件下载优化...
针对!wpk辅助哪里买,蜀山四... 针对!wpk辅助哪里买,蜀山四川怎么提高胜率,妙招教程(有挂教程)一、蜀山四川怎么提高胜率可以开透视...
五分钟辅助!扣点点脚本辅助器(... 五分钟辅助!扣点点脚本辅助器(辅助挂)好像存在有辅助技巧(有挂详情)扣点点脚本辅助器破解侠是真的助透...