简体   繁体   中英

Android : How to get the preferred (native) audio buffer size and audio sample rate in C/C++?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM