Unity – Get the max android volume with JNI without native plugin

In the previous post we learn how to get the current volume.

As I said the max volume can be different according the OS version or de device.

Now let’s get the max volume in c# without native plugin:

public int GetMaxVolume ()
{
	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 systemService = context.Call<AndroidJavaObject> ( "getSystemService", Context_AUDIO_SERVICE );
	return systemService.Call<int> ( "getStreamMaxVolume", AudioManager_STREAM_MUSIC );
}