ADB Command to set volume?

前端 未结 4 614
暗喜
暗喜 2020-12-19 23:06

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 b

4条回答
  •  -上瘾入骨i
    2020-12-19 23:34

    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. I've tried with my unrooted 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 fron this gist, change its permission to execuatble, connect your device via ADB and run the script with audio as argument:

    $ ./get_android_service_call_numbers.sh audio
    

    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
    

提交回复
热议问题