Android 通过ADB方式获取&设置音量

本文详细介绍了Android开发中如何通过dumpsys获取设备音频信息,以及使用ADB进行不同音量类型的设置,包括电话、系统、铃音、音乐和闹钟等。特别提到了Android11及以后版本MediaSessionService的使用方法。
摘要由CSDN通过智能技术生成

在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

 

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Brave & Real

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值