Search Unity

Unity & Android KeyEvent Mediabuttons

Discussion in 'Formats & External Tools' started by weto, Apr 21, 2015.

  1. weto

    weto

    Joined:
    Jul 29, 2014
    Posts:
    4
    Hi guys,

    I am trying to build an app for android which uses the Satechi Media Button as a remote. It (Satechi) can send the Android KeyEvents "KEYCODE_VOLUME_UP", "KEYCODE_VOLUME_DOWN", "KEYCODE_MEDIA_NEXT", "KEYCODE_MEDIA_PREVIOUS" and "KEYCODE_MEDIA_PLAY_PAUSE".

    I built a bridge (C#-script) and a java-script which is running in the background. When I touch the screen I want to know which button was the last to be pressed. It works for volume up and down, but the media keys get ignored. This shows that my bridge is working correctly. I set the minimum SDK to 14 (which should be enough, as the media buttons got implemented with API 11). I did so in the android manifest and in the player settings.
    Can anyone tell me how to find out where I can access (if at all) the api-level used to compile the app?

    Below you can find my onKeyDown method from the .java file
    Code (CSharp):
    1. @Override
    2. public boolean onKeyDown(int keyCode, KeyEvent event){
    3.     switch(keyCode){
    4.         case KeyEvent.KEYCODE_VOLUME_UP:
    5.             key = "VOLUME_UP";
    6.             keyInt = keyCode;
    7.             break;
    8.         case KeyEvent.KEYCODE_VOLUME_DOWN:
    9.             key = "VOLUME_DOWN";
    10.             keyInt = keyCode;
    11.             break;
    12.         case KeyEvent.KEYCODE_MEDIA_NEXT:
    13.             key = "MEDIA_NEXT";
    14.             keyInt = keyCode;
    15.             break;
    16.         case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
    17.             key = "MEDIA_PREVIOUS";
    18.             keyInt = keyCode;
    19.             break;
    20.         case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
    21.             key = "MEDIA_PLAY_PAUSE";
    22.             keyInt = keyCode;
    23.             break;
    24.         default:
    25.             key = "NOTHING";
    26.             keyInt = -2;
    27.             break;
    28.     }
    29.     if(keyInt != 0){
    30.           key = Integer.toString(keyInt);
    31.     }
    32.     return true;
    33. }
    When I hit any of the three media buttons nothing happens - not even the default case. I suspect that the KeyEvents are not recognized - although I told my project everywhere I know of that I need API 14..

    Any help is greatly appreciated
    Cheers,
    Toby

    Edit: for the bridge I followed this tutorial: http://www.lorenzonuvoletta.com/how-to-create-a-native-android-plugin-for-unity/
     
    Last edited: Apr 21, 2015
  2. cvasquez-coiron

    cvasquez-coiron

    Joined:
    Nov 10, 2014
    Posts:
    22
    Hi, were you able to properly detect the KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE event in Unity?
    Thanks