4

Is there any Adb command to set the volume to a particular value? I know that we can do

adb shell input keyevent 

for volume up and down but i want to set it to a particular value. If I change it it DB then I have to reboot the device for the changes to be reflected so i do not want to go that path. Isn't there any API where I can change the value without having to restart it and having to be dependent on Volume up and Down?

Lost
  • 12,007
  • 32
  • 121
  • 193

6 Answers6

17

media shell command can also be used:

media volume:  the options are as follows: 
    --stream STREAM selects the stream to control, see AudioManager.STREAM_*
                    controls AudioManager.STREAM_MUSIC if no stream is specified
    --set INDEX     sets the volume index value
    --adj DIRECTION adjusts the volume, use raise|same|lower for the direction
    --get           outputs the current volume
    --show          shows the UI during the volume change
examples:
    adb shell media volume --show --stream 3 --set 11
    adb shell media volume --stream 0 --adj lower
    adb shell media volume --stream 3 --get

The first example is probably the one you were looking for (but it probably didn't exist at the time of asking)

XioRcaL
  • 653
  • 2
  • 11
  • 23
  • 1
    just to improve the last sentence of this answer: "media" command does not have "volume" subcommand e.g. on Marshmallow `bash# adb shell media usage: media [subcommand] [options] media dispatch KEY media list-sessions media monitor [...]` – amonthedeamon Sep 05 '19 at 07:42
  • @amonthedeamon, I just tried it, my adb definitely has this volume subcommand listed just after the `media monitor ` I have a `media volume [options]`. Maybe It depends on the android version of the device; I only have an android 9 to test. – XioRcaL Sep 06 '19 at 08:08
  • On a Google Pixel 3a with Android 12 installed, the `media` command does not seem to exist. Calling `adb shell media` on a cmd gives me `/system/bin/sh: media: inaccessible or not found`. Any ideas? – Christian Stadelmann May 03 '22 at 11:52
4

I have used the service call audio test to set the volume on an android 2.3 device. In order to be more generic you need to research IBinder and the transaction number.

To find out what you want:

Adb shell service list packages

  • this will tell you the package file you need to look at for a service (I.e Bluetooth - com.Bluetooth.IBluetooth)

Search for the service class and 'transaction' online ("com.Bluetooth.Ibluetooth transaction")

Find the source files and find the Ibinder transaction details. This will be followed by the details of the input parameters.

I.e the first transaction on Bluetooth is .is enabled(). There are no input parameters

To use it send:

Adb shell service call Bluetooth 1

It should return a parcel containing the answer.

Remember: - I think it is only for rooted devices - the transaction number you find has an offset of 1 (transaction 0 is called with service call 'service' 1) - There are two types of input: i32 for integer or s16 for string

To set the audio there are three input integers for set volume (transaction 6)

To use it send:

Adb shell service call 7 i32 3 i32 15 i32 0 This will set the media volume to 15 (default number of levels for media audio is 15)

Clubber_81
  • 41
  • 1
4

This is an answer for anyone whose Android is too old to have volume subcommand in media command.

Thanks to Alex P's link, I got the inspiration from this guy's blog: http://ktnr74.blogspot.com/2014/09/calling-android-services-from-adb-shell.html

You can use service command to call functions like void setStreamVolume(int streamType, int index, int flags, String callingPackage) on your Android device - a SO question.
Tried with my un-rooted Android 5.1 device and it worked.

Usage: service [-h|-?]
       service list
       service check SERVICE
       service call SERVICE CODE [i32 INT | s16 STR] ...
Options:
   i32: Write the integer INT into the send parcel.
   s16: Write the UTF-16 string STR into the send parcel.

But the CODE differs between Android versions. To find the code for setStreamVolume(), first save the Bash script from this gist, change its permission to executable, connect your device via ADB and run the script with audio as argument:

$ ./get_android_service_call_numbers.sh audio

(EDIT: https://github.com/T-vK/android-svc does that script.)

The script pulls info from Google and show you a list like this.

So we know the code for setStreamVolume() is 4, since we know the number for STREAM_MUSIC is 3, we can set music volume to 7 by this command:

$ adb shell service call audio 4 i32 3 i32 7

The maximum music volume on my device is 0xF, you can query yours with int getStreamMaxVolume(int streamType) function:

$ adb shell service call audio 15 i32 3
Edward J
  • 7
  • 3
ReeseWang
  • 131
  • 1
  • 7
3

On a rooted phone you can call setMasterVolume() with service call audio <code> i32 <volume>. The codes are version specific. Let's say you want to set volume to 50% on a KitKat device. The command will be:

service call audio 9 i32 50
Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • How about on jelly bean would the code be the same? I have tried setting the volume on a jelly bean device using this method and it doesn't seem to work? – TylerD87 Mar 27 '14 at 11:27
  • 1
    read http://ktnr74.blogspot.com/2014/09/calling-android-services-from-adb-shell.html to find out the proper code for your android version – Alex P. Dec 10 '14 at 00:30
3

For those of you struggling with the answer provided by XioRCal, use adb shell cmd media_session instead of adb shell media as it has been removed in Android 11 and 12.

Reference: "media: inaccessible or not found" error when attempting to control device volume via ADB

0

In android 12 or later, can use cmd media_session shell cmd.
below is the usage: (need replace some media to cmd media_session)

usage: media_session [subcommand] [options]
       media_session dispatch KEY
       media_session dispatch KEY
       media_session list-sessions
       media_session monitor <tag>
       media_session volume [options]

media_session dispatch: dispatch a media key to the system.
                KEY may be: play, pause, play-pause, mute, headsethook,
                stop, next, previous, rewind, record, fast-forword.
media_session list-sessions: print a list of the current sessions.
media_session monitor: monitor updates to the specified session.
                       Use the tag from list-sessions.
media_session volume:  the options are as follows: 
                --stream STREAM selects the stream to control, see AudioManager.STREAM_*
                                controls AudioManager.STREAM_MUSIC if no stream is specified
                --set INDEX     sets the volume index value
                --adj DIRECTION adjusts the volume, use raise|same|lower for the direction
                --get           outputs the current volume
                --show          shows the UI during the volume change
        examples:
                adb shell media volume --show --stream 3 --set 11
                adb shell media volume --stream 0 --adj lower
                adb shell media volume --stream 3 --get
Huanfeng
  • 21
  • 2