Compile FFmpeg with OpenSL ES on Android

This guide provides a straightforward walkthrough on how to compile the FFmpeg multimedia framework with native OpenSL ES audio support for Android applications. You will learn how to set up your environment with the Android NDK, configure the FFmpeg build options to enable the OpenSL ES driver, and run the compilation script to generate the required libraries.

Prerequisites

Before beginning the compilation process, ensure you have the following prepared on your development machine (Linux or macOS is recommended):

Step 1: Set Environment Variables

You need to point your build environment to the Android NDK toolchain. Create a shell script (e.g., build_ffmpeg.sh) inside the root of your FFmpeg source directory and define the following variables:

# Set this to your local Android NDK path
NDK=/path/to/your/android-ndk

# Define target architecture and API level
API=21
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
SYSROOT=$TOOLCHAIN/sysroot

# Target triple for ARM64 (adjust for other architectures if needed)
TARGET=aarch64-linux-android
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++

(Note: Replace linux-x86_64 with darwin-x86_64 if you are compiling on macOS).

Step 2: Configure FFmpeg with OpenSL ES

To enable OpenSL ES support, you must explicitly pass the --enable-opensles flag to the FFmpeg configure script. You also need to specify the Android target OS and the cross-compilation toolchain.

Add the configuration block to your build script:

./configure \
  --prefix=./android/arm64-v8a \
  --target-os=android \
  --arch=aarch64 \
  --cpu=armv8-a \
  --enable-cross-compile \
  --cc=$CC \
  --cxx=$CXX \
  --sysroot=$SYSROOT \
  --extra-cflags="-O3 -fPIC" \
  --extra-ldflags="" \
  --disable-shared \
  --enable-static \
  --disable-doc \
  --disable-ffmpeg \
  --disable-ffplay \
  --disable-ffprobe \
  --enable-opensles \
  --enable-jni

The key flag here is --enable-opensles, which instructs FFmpeg to compile the native Android audio driver. The --enable-jni flag is also recommended as it allows FFmpeg to interact with the Android Java environment.

Step 3: Compile the Code

Once the configuration options are set, append the compile commands to the end of your script:

# Clean previous builds
make clean

# Compile using multiple CPU cores
make -j$(nproc)

# Install the headers and binaries to the prefix directory
make install

Step 4: Run the Script

Make your script executable and run it from your terminal:

chmod +x build_ffmpeg.sh
./build_ffmpeg.sh

Upon successful compilation, you will find the generated static libraries (.a files) and header files inside the ./android/arm64-v8a directory. These libraries are now ready to be linked into your Android project via CMake or ndk-build, allowing your application to utilize low-latency OpenSL ES audio playback and recording.