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

05-14 1451阅读

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

Android 通过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

     

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]