Android: Access AudioRecord.Builder class from JNI level similar to AudioRecord

Viewed 224

What is right way to access AudioRecord.Builder at the JNI level?

Similar to AudioRecord class.

(jclass)jni_env->NewGlobalRef(jni_env->FindClass("android/media/AudioRecord"));

ex:

AudioRecord recorder = new AudioRecord.Builder()
         .setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
         .setAudioFormat(new AudioFormat.Builder()
                 .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                 .setSampleRate(32000)
                 .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
                 .build())
         .setBufferSizeInBytes(2*minBuffSize)
         .build();
1 Answers

Besides my above comment, you can easily find out these types of things as android is open source.

So first take a look at the android AudioRecord source code. In there you will notice the AudioRecord constructor calls a method called native_start as well as other native methods to setup the audio via JNI.

enter image description here

browsing further you will see a comment referencing the AudioRecord.cpp file.

The cpp file can be found here you might need to make sure you take a look at the one that matches your android version, I just picked anyone from a Google search. In there you will of course see how the AudioRecord class functions natively, as well as what methods it exposes to JNI etc

enter image description here

With the above you can essentially see the AudioRecord java class essentially interacts with the native code, which is what you want to achieve

UPDATE:

So from your comment you can do exactly this. The issue is I just took random links so sorry maybe my fault.

If you look at the master branch source code you will find what you are looking for.

In the AudioRecord.java in the master branch you will find it accepts a AudioAttributes class in the constructor, which undergoes some filtering further down and then finally native_setup is called with these filtered audio attributesmAudioAttributes:

enter image description here

You could further see how ithese attributes are being applied in the native code AudioRecord.cpp by following the trail of the jaa variable passed into the native_setup via AudioRecord.java which maps to the native method android_media_AudioRecord_setup :

enter image description here

enter image description here

Related