page.title=High-Performance Audio Basics @jd:body

On this page

  1. Building Great Audio Apps
  2. Adding OpenSL ES to Your App
  3. Building and Debugging
  4. Audio Power Consumption
  5. Samples

Video

Google I/O 2013 - High Performance Audio

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 the typical use cases for these high-performance audio APIs, how to add them into your app's source code, and how to incorporate them into the build process.

Building Great Audio Apps

The OpenSL ES APIs are available to help you develop and improve your app's audio performance. Some typical use cases include the following:

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>

When you include the {@code OpenSLES_Android.h} header file, the following headers are included automatically:

#include <SLES/OpenSLES_AndroidConfiguration.h>
#include <SLES/OpenSLES_AndroidMetadata.h>

Note: These headers are not required, but are shown as an aid in learning the API.

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.

Audio Power Consumption

Constantly outputting audio incurs significant power consumption. Ensure that you stop the output in the onPause() method. Also consider pausing the silent output after some period of user inactivity.

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.