4

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.

user1884325
  • 2,530
  • 1
  • 30
  • 49
  • I've worked with OpenSL before and I was in need of exact same thing like you. It would be better if we could fetch the preferred sampling rate in native side as the Java API supports API level 17 or higher. So I posted exact problem of mine and then solved it myself. You can have a look at this question and answer too. This might help you, if you have the similar situation. http://stackoverflow.com/questions/22342040/voice-communication-at-8khz-sampling-rate-for-all-android-device-using-opensl – Reaz Murshed Feb 07 '16 at 09:46
  • SLAudioOutputDescriptor and SLAudioInputDescriptor are not implemented on Android. Reaz's answer looks like a good summary of current best practices. – Ian Ni-Lewis May 13 '16 at 19:40

1 Answers1

1

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.

Over17
  • 961
  • 7
  • 8