In my last post I described how we use JNI and the Java AudioTrack class to output audio from native code on Android versions prior to 2.3 (that is, before the release of OpenSL ES). But actually, that’s only half the story.
OpenSL ES code is in the libOpenSLES.so shared library. If you link to this library, though, then your executable will try to load it when it starts up, and if it’s running in a version of Android before 2.3, then that library will not exist, and the executable will fail to start, even if you were never going to actually call any of the OpenSL ES code.
One solution is to delay loading of the shared library until we’re sure we actually can; that is, until we know we’re on Android 2.3 or later. This can be done with the functions dlopen() and dlsym().
Rather than linking to libOpenSLES.so on the link line, use dlopen() to load it at runtime when you’ve verified that SDK version. Use dlsym() to look up pointers to the functions and data you’re going to need; and use dlclose() when you shut down.
This is awkward, but it’s made somewhat easier by the design of OpenSL ES; there is actually only one exported function (slCreateEngine), plus several SL_IID constants.
Note also that the other big API released in 2.3 is the native asset manager, so you’ll have to go through a similar process if you want to read assets also.

About

I believe it should say “In my last post I described how we use NDK and the Java AudioTrack…”, rather than “In my last post I described how we use JNI and the Java AudioTrack…”. Funny typo, oh and thanks for all these little infos. Great!
Oops. Actually my suggestion is also wrong. You just described how you use the AudioTrack class as part of the Android SDK. So once more: “In my last post I described how we use the Android SDK and the AudioTrack class…”.
However to really set thinks clear, let me quickly elaborate on the JNI (or just have a look at http://en.wikipedia.org/wiki/Java_Native_Interface): the JNI is an interface, that allows you to use C or C++ code when using Java, “plain” Java that is. It doesn’t work with Android, there you have the NDK for that kind of thing.
Anyway. I hope I manage to get everybody confused by now, although originally I had the opposite intention.
Cheers, Audiodroid.
Thanks for the comment – but in fact, you can use JNI on Android (and we do). You can use it to call native code (built using the NDK) from Java code, or to call Java code from native code.
Or put more simply:
NDK = interface for native code (C or C++)
SDK = interface for Java code
and the JNI ties the native and Java code together.
Pingback: How to play sound or music from C in Android without java? : Android Community - For Application Development