title: How to get audio volume on Android using JNI from native code?
body: <p>I'm trying to get the audio volume on Android using JNI from C++. My constraint is that I don't have any access to the Java part. </p>
  
  <p>I found a way to make my JNI query from this site: <a href="https://github.com/ouya/ouya-sdk-examples/blob/master/Android/AndroidVirtualControllerNativeActivity/jni/AudioManager.cpp" rel="nofollow">AudioManager.cpp</a></p>
  
  <p>but I supposed I did something wrong with my last call in this code:</p>
  
  <pre><code>jclass AudioManager = env-&gt;FindClass("android/media/AudioManager");
  
  jmethodID getStreamVolume = env-&gt;GetMethodID(AudioManager, "getStreamVolume", "()I");
  
  jint stream = 3; //STREAM_MUSIC
  int volume = env-&gt;CallIntMethod(AudioManager, getStreamVolume, stream); //Crash here
  </code></pre>
  
  <p>In this code, I'm trying to get the audio volume from STREAM_MUSIC identified by id=3 in the Android documentation: <a href="http://developer.android.com/reference/android/media/AudioManager.html#getStreamVolume(int)" rel="nofollow">http://developer.android.com/reference/android/media/AudioManager.html#getStreamVolume(int)</a></p>
  
  <h2>EDIT: This is my working code:</h2>
  
  <pre><code>jclass context = env-&gt;FindClass("android/content/Context");
  
  jfieldID audioServiceField = env-&gt;GetStaticFieldID(context, "AUDIO_SERVICE", "Ljava/lang/String;");
  
  jstring jstr = (jstring)env-&gt;GetStaticObjectField(context, audioServiceField);
  
  jmethodID getSystemServiceID = env-&gt;GetMethodID(context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
  
  jobject tm = env-&gt;CallObjectMethod(currentAndroidActivity, getSystemServiceID, jstr);
  
  jclass AudioManager = env-&gt;FindClass("android/media/AudioManager");
  
  jmethodID getStreamVolume = env-&gt;GetMethodID(AudioManager, "getStreamVolume", "(I)I");
  
  jint stream = 3; //MUSIC_STREAM = 3
  int volume = env-&gt;CallIntMethod(tm, getStreamVolume, stream);
  DEBUG_LOG(DEBUG_TAG, "volume = " + to_string(volume));
  </code></pre>