How to integrate Exoplayer FFmpeg extension

How enrich the Exoplayer is I think you know it. It has a broad area of supported things related to playing videos in Android.

By default android does not support some audio and video codecs and so Exoplayer. This is perfectly ok. Exoplayer has a different work around for this problem.

I think you have heard about FFmpeg. Its a command line tool for convert audio and video codec. Exoplayer uses FFmpeg to support various type of audios only. For example if a video file contains ac3 audio then the video will keep playing without any sound. 

Today we are going to learn how to integrate Exoplayer ffmpeg extension in your android app. This tutorial is done with Ubuntu 18.04, Android studio 4.0 and NDK r20.

Supported formats

If you use ffmpeg extension you will be covering up to these audio formats listed below. You can use all of formats. But you have to explicitly which formats you are gonna build.

Vorbisvorbis
Opusopus
FLACflac
ALACalac
PCM μ-lawpcm_mulaw
PCM A-lawpcm_alaw
MP1, MP2, MP3mp3
AMR-NBamrnb
AMR-WBamrwb
AACaac
AC-3ac3
E-AC-3eac3
DTS, DTS-HDdca
TrueHDmlp truehd

Step 1: Download NDK r20

First thing first. This tutorial is done with ndk version r20. You can also try some other version if you want. But it recommended to use r20. Download is here.

Step 2: Cloning the Demo Project

We will have to clone the demo project of exoplayer.

git clone https://github.com/google/ExoPlayer.git

After cloning the repo  get in the folder using cd Exoplayer.

  • Now set the shell variable.
    FFMPEG_EXT_PATH="$(pwd)/extensions/ffmpeg/src/main/jni"
  • Set NDK path
    NDK_PATH="<Path to android ndk>"
  • Set the host platform (use “darwin-x86_64” for Mac OS X)
    HOST_PLATFORM="linux-x86_64"
  • Now add decoder whichever you want. Note that you can add multiple decoder using space in command. We are going to add 3 decoders for example now: mp3, aac and ac3.
    ENABLED_DECODERS=(mp3 aac ac3)
  • Now we will download the and build the FFmpeg 4.2 for armeabi-v7aarm64-v8ax86 and x86_64. It will take some time.
    cd "${FFMPEG_EXT_PATH}" && \
    ./build_ffmpeg.sh \
      "${FFMPEG_EXT_PATH}" "${NDK_PATH}" "${HOST_PLATFORM}" 
      "${ENABLED_DECODERS[@]}"
  • Now build the library
    cd "${FFMPEG_EXT_PATH}" && \
    ${NDK_PATH}/ndk-build APP_ABI="armeabi-v7a arm64-v8a x86 x86_64" -j4

Step 3: Build the .aar

Jni building is done. Now we will build the .aar from the demo project. Open the project in your terminal. Run the below command:
./gradlew :extension-ffmpeg:assembleRelease
Now the .aar file should be generated in /ExoPlayer/extensions/ffmpeg/buildout/outputs/aar/extension-ffmpeg-release.aar Now you can use this .aar in your project and you will be able to play the audio formats.

Download the aar file

If you think its too much difficult to do all the jobs or you did not understand all the steps no worries. You can download my pre generated .aar here. The problem is it will only add the .aac codec in to your player.

 

At next we will learn how to play encrypted video in android exoplayer