The reason why I need this extension is because I found that Exoplayer does not support AC3 audio when I was making the player. After several twists and turns, I finally got it done. Write it down and record it

Build FFMPEG (Linux)

Compilation platform: Ubuntu 16.04LTS

Ffmpeg version: release/4.2

NDK version: Android-NDK-R20B

  1. Download NDK, I use The Google-recommended Android-NdK-R20b, pay attention to download Linux version, download and extract to the folder. My NDK folder is /home/ye/ndk/android-ndk-r20b

  2. Clone FFmpeg and switch branches to Release /4.2, also the 4.2 version recommended by Google

    Git clone git checkout https://git.videolan.org/git/ffmpeg.git release / 4.2Copy the code
  3. Open the folder where ffmpeg is located (for example, my folder is /home/ye/project) and create a new file: build_ffmpeg.sh

  4. Copy the build_ffmpeg.sh file in the ExoPlayer and paste it into your build_ffmpeg.sh file and modify it as follows (the script is attached at the bottom) :

  5. Sudo./build_ffmpeg.sh, generate so library in ffmpeg/android-libs directory

If C Compiler test failed is displayed after the command is executed, it indicates that the path configuration is incorrect

Libffmpeg_jni. so (Windows)

I thought it was over after compiling four libraries, but I never thought it would work in the project. The FfmpegAudioRenderer was found empty in the log. A check of FfmpegLibrary shows that libffmpeg_jin. So is missing and libavresample.so is redundant.

As a result, I could not find the document and did not understand the cmake rules, wasting a lot of time

  1. Download android-dk-r20b, note that Windows is selected this time, unzip, configure the path in AndroidStudio

  2. clone ExoPlayer

     git clone https://github.com/google/ExoPlayer.git	
    Copy the code
  3. Clone ffmpeg and switch branches to Release /4.2

    Git clone git checkout https://git.videolan.org/git/ffmpeg.git release / 4.2Copy the code
  4. Copy ffmPEG to jNI folder in ffmpeg module in ExoPlayer

  5. Copy the Android-Libs compiled in Liunx into the FFMPEG folder

  6. Configure cmakelists. TXT location, modify build.gradle, add red box inside configuration

  7. Compile, Gradle – > the extension – ffmpeg – > Tasks – > build – > assemble

  8. Fixed error: ‘libavutil/avconfig.h’ file not found

    Create a new header file avconfig.h in the ffmpeg/libavutil folder and add the following contents to the default contents

     /* Generated by ffconf */
     #ifndef AVUTIL_AVCONFIG_H
     #define AVUTIL_AVCONFIG_H
     #define AV_HAVE_BIGENDIAN 0
     #define AV_HAVE_FAST_UNALIGNED 0
     #endif /* AVUTIL_AVCONFIG_H */
    Copy the code

  9. Repeat 5 in buildoutput/imtermediates/cmake/release/obj have each kernel so library, containing the libffmpeg_jni. So

I felt like I could do this on Linux, but I tried it myself, introduced a lot of header files, and ended up reporting errors, and finally found a way to compile on Windows. If anyone knows how to do that, you’re welcome to add.

Use it in your own projects

  1. Move the four so libraries (libavcodec.so, libavutil. So, libffmpeg_jni.so, libswresample.so) to the libs folder of your project (remember to configure sourceSets and abiFilter in build.gradle) S)

  2. Copy ExoPlayer ffmpeg extension under the Java folder to the corresponding position (com. Google. Android. Exoplayer2. Ext ffmpeg)

  3. Create a new RenderersFactory file, add the FfmpegAudioRenderer, and enable the extension

  4. Build the player using the Renderer

  5. Play successfully! The AC3 audio is finally playing properly, and the FfmpegAudioRenderer has been added to the log.

Build_ffmpeg.sh (build_ffmpeg.sh)

#! /bin/bash # # Copyright (C) 2019 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the  License. # # 1. COMMON_OPTIONS=" --target-os=android --disable-static --enable-shared --disable-doc --disable-programs --disable-everything --disable-avdevice --disable-avformat --disable-swscale --disable-postproc --disable-avfilter --disable-symver --disable-avresample --enable-swresample --enable-avresample --enable-decoder=vorbis --enable-decoder=opus --enable-decoder=flac --enable-decoder=alac --enable-decoder=pcm_mulaw --enable-decoder=pcm_alaw --enable-decoder=mp3 --enable-decoder=amrnb --enable-decoder=amrwb --enable-decoder=aac --enable-decoder=ac3 --enable-decoder=eac3 --enable-decoder=dca --enable-decoder=mlp --enable-decoder=truehd --extra-ldexeflags=-pie "&& \ # 2. The NDK path, NDK_PATH="/home/ye/ NDK /android-ndk-r20b" HOST_PLATFORM="linux-x86_64" TOOLCHAIN_PREFIX = "${NDK_PATH} / toolchains/LLVM/prebuilt / ${HOST_PLATFORM} / bin" # 3. FFMPEG path, Remember to modify CD "/home/ye/Project/ffmpeg"./configure \ --libdir=android-libs/armeabi-v7a \ --arch=arm \ -- CPU =armv7-a \ --cross-prefix="${TOOLCHAIN_PREFIX}/armv7a-linux-androideabi16-" \ --nm="${TOOLCHAIN_PREFIX}/arm-linux-androideabi-nm" \  --strip="${TOOLCHAIN_PREFIX}/arm-linux-androideabi-strip" \ --extra-cflags="-march=armv7-a -mfloat-abi=softfp" \ --extra-ldflags="-Wl,--fix-cortex-a8" \ ${COMMON_OPTIONS} make -j4 make install-libs make clean ./configure \ --libdir=android-libs/arm64-v8a \ --arch=aarch64 \ --cpu=armv8-a \ --cross-prefix="${TOOLCHAIN_PREFIX}/aarch64-linux-android21-" \ --nm="${TOOLCHAIN_PREFIX}/aarch64-linux-android-nm" \ --strip="${TOOLCHAIN_PREFIX}/aarch64-linux-android-strip" \ ${COMMON_OPTIONS} make -j4 make install-libs make clean ./configure \ --libdir=android-libs/x86 \ --arch=x86 \ --cpu=i686 \ --cross-prefix="${TOOLCHAIN_PREFIX}/i686-linux-android16-" \ --nm="${TOOLCHAIN_PREFIX}/i686-linux-android-nm" \ --strip="${TOOLCHAIN_PREFIX}/i686-linux-android-strip" \ --disable-asm \ ${COMMON_OPTIONS} make -j4 make install-libs make clean ./configure \ --libdir=android-libs/x86_64 \ --arch=x86_64 \ --cpu=x86_64 \ --cross-prefix="${TOOLCHAIN_PREFIX}/x86_64-linux-android21-" \ --nm="${TOOLCHAIN_PREFIX}/x86_64-linux-android-nm" \ --strip="${TOOLCHAIN_PREFIX}/x86_64-linux-android-strip" \ --disable-asm \ ${COMMON_OPTIONS} make -j4 make install-libs make cleanCopy the code