Search Unity

How do I get the volume up and down buttons?

Discussion in 'Android' started by Sahkan, Aug 2, 2015.

  1. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    I know KeyCode.ESC is the back button.
    But how do I can the volume up and down buttons?
    I'v noticed that while in Unity's game, the volume buttons will make no changes in the volume, they just won't do a thing.
    So I guess if this is canceled, there got to be a keycode for it right?
    I'm using Unity5.1.2
     
  2. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    Anyone?...
     
  3. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    No one knows? no ***** way!
     
  4. roadis

    roadis

    Joined:
    Jul 3, 2012
    Posts:
    43
    Vol up and down are controlled by the OS. Its not implemented in Unity. What you can use is a Android Plugin that u build for yourself.

    Here is the Android Code for that:

    Code (Java):
    1.  
    2. @Override
    3. public boolean dispatchKeyEvent(KeyEvent event) {
    4.     int action = event.getAction();
    5.     int keyCode = event.getKeyCode();
    6.         switch (keyCode) {
    7.         case KeyEvent.KEYCODE_VOLUME_UP:
    8.             if (action == KeyEvent.ACTION_DOWN) {
    9.                 //TODO
    10.             }
    11.             return true;
    12.         case KeyEvent.KEYCODE_VOLUME_DOWN:
    13.             if (action == KeyEvent.ACTION_DOWN) {
    14.                 //TODO
    15.             }
    16.             return true;
    17.         default:
    18.             return super.dispatchKeyEvent(event);
    19.         }
    20.     }
     
  5. Sahkan

    Sahkan

    Joined:
    Mar 3, 2013
    Posts:
    204
    If this is controlled by the OS why isn't it work when I try to change the volume with game built with Unity5.1.2? If Unity blocks it, it better give access to it isn't it?
    And how exactly do I make the plugin that uses this code?
     
    kamran-bigdely likes this.
  6. Wapakzinggerzy4TW

    Wapakzinggerzy4TW

    Joined:
    Jan 7, 2014
    Posts:
    1
    Anyone figure this out? :(
     
    kamran-bigdely likes this.
  7. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,918
    Unity shouldn't consume volume up/volume down events as far as I can tell. Please provide more information:
    * what Unity version are you using?
    * if you exit your application and then press volume up/volume down, and then get back to your application, does the sound changes then?
     
  8. kamran-bigdely

    kamran-bigdely

    Joined:
    Jun 19, 2014
    Posts:
    25
    Anyway to detect whether the user wants louder music or otherwise in Unity?

    AudioListener.volume
    value does not change if the player presses volume up or down buttons.
     
  9. MaryamKamel

    MaryamKamel

    Joined:
    Feb 5, 2016
    Posts:
    22
    you can check the native binding in unity's user manual
     
  10. Onlyhack

    Onlyhack

    Joined:
    Oct 31, 2017
    Posts:
    2
    it only java code, not use for unity
     
  11. fahruz

    fahruz

    Joined:
    Mar 5, 2020
    Posts:
    57
    I'm also interested in how to detect when the volume up button is pressed. I have a remote shutter control for phone cameras that basically sends a "volume up" event via Bluetooth and I want to detect the ON/OFF status of that button in a Unity script on Windows. Is that possible and if yes, how?
     
  12. valkindo123

    valkindo123

    Joined:
    Aug 5, 2020
    Posts:
    2
    Were you able to detect the volume buttons?
     
  13. fahruz

    fahruz

    Joined:
    Mar 5, 2020
    Posts:
    57
    No, I used a key redirection utility to redirect to a key that Unity recognises.