How to build FFmpeg Extension for ExoPlayer within Windows
Google's LLM decided to use ExoPlayer for the media player, which is the commonly used media player for Android development. After some testing, it seemed that my phone (Oneplus Nord CE 2) did not support all audio codecs, resulting in some streams not having audio.
When an audio codec can not be decoded in hardware, software decoders come in place. The ExoPlayer comes with a FFmpeg extension. The big downside, it has to be build locally.
Prerequisites
- Android Studio (SDK + NDK r26.x)
- WSL (Ubuntu)
Within WSL...
1. Clone Media3 (ExoPlayer)
cd ~
git clone https://github.com/androidx/media.git
cd media/libraries/decoder_ffmpeg/src/main/jni
2. Setup Environment (WSL)
Download the NDK for Linux as well and place it in the users '~' directory: https://github.com/android/ndk/wiki
cd ~
wget https://dl.google.com/android/repository/android-ndk-r26d-linux.zip
unzip android-ndk-r26d-linux.zip
FFMPEG_MODULE_PATH=~/media/libraries/decoder_ffmpeg/src/main/jni/
NDK_PATH=~/android-ndk-r26d # Linux NDK (recommended)
HOST_PLATFORM=linux-x86_64
ANDROID_ABI=24
ENABLED_DECODERS=(eac3 mp3 ac3 aac opus)
3. Build FFmpeg
./build_ffmpeg.sh \
"${FFMPEG_MODULE_PATH}" \
"${NDK_PATH}" \
"${HOST_PLATFORM}" \
"${ANDROID_ABI}" \
"${ENABLED_DECODERS[@]}"
✔ Output: static libraries (.a)
4. Move the media folder from WSL to Windows
Move it to the same folder where your AndroidStudioProjects project lives, e.g:
mv ~/media/ /mnt/c/Users/bramn/AndroidStudioProjects/Media/
5. Android Integration
settings.gradle.kts
// Media3 local dependency setup
gradle.extra.set("androidxMediaModulePrefix", "media-")
apply(from = file("../media/core_settings.gradle"))
build.gradle.kts
// Local Media3 dependencies as per https://github.com/androidx/media#locally
implementation(project(":media-lib-exoplayer"))
// implementation(project(":media-lib-exoplayer-hls"))
// implementation(project(":media-lib-exoplayer-dash"))
implementation(project(":media-lib-ui"))
implementation(project(":media-lib-decoder-ffmpeg"))
6. Enable FFmpeg in Player
new DefaultRenderersFactory(context)
.setExtensionRendererMode(
DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON);
7. Output
- FFmpeg build →
.a(static libs) - Android build →
.so(final native libs)
Check:
find app/build -name "*.so"
Summary
- Build FFmpeg in WSL using Linux NDK
- Integrate module into project
- Android build generates final
.so - Warnings during build are normal
Usefull links
https://developer.android.com/media/media3/exoplayer/supported-formats#ffmpeg-library https://github.com/android/ndk/wiki