Reputation: 2550
To get the preferred audio buffer size and audio sample rate for a given Android device, you can execute the following Java code:
// To get preferred buffer size and sampling rate.
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
String rate = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
String size = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
Log.d("Buffer Size and sample rate", "Size :" + size + " & Rate: " + rate);
Is it possible to do this in C/C++ instead?
If so, how?
The openSLES API doesn't seem to offer this feature. I did notice that openSLES.h defines an SLAudioInputDescriptor struct as well as an SLAudioOutputDescriptor, but if this is available on Android I am not sure how to get the supported microphone and speaker sample rates. Also, the struct doesn't contain info about preferred buffer size.
Upvotes: 4
Views: 2948
Reputation: 1071
Of course this is not a part of OpenSL ES API, these params are Android-specific and are a part of Android OS.
Unfortunately I've found no way to get these params directly in native code. I ended up calling Java functions through JNI.
Here comes the pseudo code:
jclass audioSystem = env->FindClass("android/media/AudioSystem");
jmethodID method = env->GetStaticMethodID(audioSystem, "getPrimaryOutputSamplingRate", "()I");
jint nativeOutputSampleRate = env->CallStaticIntMethod(audioSystem, method);
method = env->GetStaticMethodID(audioSystem, "getPrimaryOutputFrameCount", "()I");
jint nativeBufferLength = env->CallStaticIntMethod(audioSystem, method);
Add the error handling as you would like it to be.
Upvotes: 1