page.title=OpenSL ES™ Basics @jd:body

On this page

  1. Adding OpenSL ES to Your App
  2. Building and Debugging
  3. Samples

The Khronos Group's OpenSL ES standard exposes audio features similar to those in the {@link android.media.MediaPlayer} and {@link android.media.MediaRecorder} APIs in the Android Java framework. OpenSL ES provides a C language interface as well as C++ bindings, allowing you to call it from code written in either language.

This page describes how to add these audio APIs into your app's source code, and how to incorporate them into the build process.

Adding OpenSL ES to your App

You can call OpenSL ES from both C and C++ code. To add the core OpenSL ES feature set to your app, include the {@code OpenSLES.h} header file:

#include <SLES/OpenSLES.h>

To add the OpenSL ES Android extensions as well, include the {@code OpenSLES_Android.h} header file:

#include <SLES/OpenSLES_Android.h>

Building and Debugging

You can incorporate OpenSL ES into your build by specifying it in the {@code Android.mk} file that serves as one of the NDK build system's makefiles. Add the following line to {@code Android.mk}:

LOCAL_LDLIBS += -lOpenSLES

For robust debugging, we recommend that you examine the {@code SLresult} value that most of the OpenSL ES APIs return. You can use asserts or more advanced error-handling logic for debugging; neither offers an inherent advantage for working with OpenSL ES, although one or the other might be more suitable for a given use case.

We use asserts in our examples, because they help catch unrealistic conditions that would indicate a coding error. We have used explicit error handling for other conditions more likely to occur in production.

Many API errors result in a log entry, in addition to a non-zero result code. Such log entries can provide additional detail that proves especially useful for relatively complex APIs such as {@code Engine::CreateAudioPlayer}.

You can view the log either from the command line or from Android Studio. To examine the log from the command line, type the following:

$ adb logcat

To examine the log from Android Studio, either click the Logcat tab in the Debug window, or click the Devices | logcat tab in the Android DDMS window.

Samples

Supported and tested example code that you can use as a model for your own code resides both locally and on GitHub. The local examples are located in {@code platforms/android-9/samples/native-audio/}, under your NDK root installation directory. On GitHub, they are available from the {@code android-ndk} repository, in the {@code audio-echo} and {@code native-audio} directories.

The Android NDK implementation of OpenSL ES differs from the reference specification for OpenSL ES 1.0.1 in a number of respects. These differences are an important reason as to why sample code that you copy directly from the OpenSL ES reference specification may not work in your Android app.

For more information on differences between the reference specification and the Android implementation, see OpenSL ES™ for Android.