Skip to content

Most visited

Recently visited

navigation

Getting Started

This section provides the information needed to get started using the OpenSL ES APIs.

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 OpenSLES.h header file:

#include <SLES/OpenSLES.h>

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

#include <SLES/OpenSLES_Android.h>

When you include the 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 Android.mk file that serves as one of the NDK build system's makefiles. Add the following line to Android.mk:

LOCAL_LDLIBS += -lOpenSLES

For robust debugging, we recommend that you examine the 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 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.

Example code

We recommend using supported and tested example code that is usable as a model for your own code, which is located in the NDK folder platforms/android-9/samples/native-audio/, as well as in the audio-echo and native-audio folders of the android-ndk GitHub repository.

Caution: The OpenSL ES 1.0.1 specification contains example code in the appendices (see Khronos OpenSL ES Registry for more details). However, the examples in Appendix B: Sample Code and Appendix C: Use Case Sample Code use features that are not supported by Android. Some examples also contain typographical errors, or use APIs that are likely to change. Proceed with caution when referring to these; though the code may be helpful in understanding the full OpenSL ES standard, it should not be used as-is with Android.

Audio content

The following are some of the many ways to package audio content for your application:

Note: Finding or creating useful audio content for your application is beyond the scope of this article. You can use web search terms such as interactive audio, game audio, sound design, and audio programming to locate more information.

Caution: It is your responsibility to ensure that you are legally permitted to play or record content. There may be privacy considerations for recording content.

Code samples

These sample apps are available on our GitHub page:

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 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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)