Mobile Development 6 min read

Controlling Android Device Volume via AudioManager and ADB Commands

This article explains how to programmatically adjust and query Android device volume using AudioManager APIs, required permissions, and ADB shell commands across different Android versions, providing code examples and command generation logic for both newer (≥8.0) and older devices.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Controlling Android Device Volume via AudioManager and ADB Commands

In the 360 testing platform, it is necessary to control the volume of Android devices to prevent audio from videos, music, or games from disturbing the office environment while still allowing audio and video recording scripts to run.

Android developers can use AudioManager.setStreamMute and AudioManager.getStreamVolume to adjust volume, but on Android 7.0+ the app must request the ACCESS_NOTIFICATION_POLICY and MODIFY_PHONE_STATE permissions, which are difficult for automation scripts.

The article examines the source code of AudioManager , showing that it ultimately calls the system service IAudioService.setStreamVolume with three parameters: int streamType , int index , and int flags .

Because reflective calls are complex, the author demonstrates using ADB commands to control volume directly. For Android 8.0 (API 26) and above, the command is:

adb shell media volume --show --stream 3 --set 1

For Android 5.1 (API 22) the command is:

adb shell service call audio 4 i32 <streamType> i32 <index> i32 0

To query the current volume, the following ADB calls are used:

service call audio 13 i32 <streamType>

The article provides Java helper methods that generate the appropriate command strings based on the device’s SDK version:

public String setAudioCMD(int type, int volume) {
    String cmd = "";
    if (Build.VERSION.SDK_INT == 22) {
        cmd = String.format("service call audio 4 i32 %d i32 %d i32 0", type, volume);
    } else if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT <= 25) {
        cmd = String.format("service call audio 3 i32 %d i32 %d i32 0", type, volume);
    } else if (Build.VERSION.SDK_INT >= 26) {
        cmd = String.format("media volume --show --stream %d --set %d", type, volume);
    }
    return cmd;
}

public String getAudioCMD(int type) {
    String cmd = "";
    if (Build.VERSION.SDK_INT == 22) {
        cmd = String.format("service call audio 13 i32 %d", type);
    } else if (Build.VERSION.SDK_INT == 23) {
        cmd = String.format("service call audio 9 i32 %d", type);
    } else if (Build.VERSION.SDK_INT == 24 || Build.VERSION.SDK_INT == 25) {
        cmd = String.format("service call audio 8 i32 %d", type);
    } else if (Build.VERSION.SDK_INT >= 26) {
        cmd = String.format("media volume --show --stream %d --get", type);
    }
    return cmd;
}

Sample output shows successful volume control for devices with Android ≥8.0 (e.g., volume 6 in range 0‑15) and for older devices where the raw parcel result must be interpreted (e.g., hex 0x00000009 equals volume 9).

References include a link to an Android source code browsing site for further investigation.

AndroidMobileDevelopmentAudioManagerADBShellCommandsVolumeControl
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.