To get the current volume value on Android in Unity without plugin you can use this c# script.
Thanks to the Java Native Interface.
You will get an integer between 0 and ?, 15, 13, 25, …
NB: Actually the max volume value depends of the OS version.
1 2 3 4 5 6 7 8 9 10 11 12 |
public int GetVolume () { AndroidJavaObject currentActivity = unityPlayerClass.GetStatic ( "currentActivity" ); AndroidJavaObject packageManager = currentActivity.Call ( "getPackageManager" ); AndroidJavaObject context = currentActivity.Call ( "getApplicationContext" ); AndroidJavaClass audioManagerClass = new AndroidJavaClass ( "android.media.AudioManager" ); AndroidJavaClass contextClass = new AndroidJavaClass ( "android.content.Context" ); int AudioManager_STREAM_MUSIC = audioManagerClass.GetStatic<int> ( "STREAM_MUSIC" ); string Context_AUDIO_SERVICE = contextClass.GetStatic<string> ( "AUDIO_SERVICE" ); AndroidJavaObject audioService = context.Call<AndroidJavaObject> ( "getSystemService", Context_AUDIO_SERVICE ); return audioService.Call<int> ( "getStreamVolume", AudioManager_STREAM_MUSIC ); } |
Bonjour,
Merci pour cet article. Bon choix de sujet. Il est important de choisir le plugin idéal pour que tout soient optimal.
Bonjour,
Très bon sujet. Il serait bien de la traduire pour que tous puisse comprendre le fond.
Merci
Hello,
Thank you very much for this tutorial… I am a newbie and trying to catch Android volume key events in Unity without plugin… Is it possible?
Thank You