ExoPlayer+FFMpeg软解

概述
ExoPlayer是Google开源的Android平台媒体播放器。 它是Android的MediaPlayer API的替代品。
相比于IJKPlayer,EXOPlayer更易于拓展,功能更多,bug更少。ExoPlayer虽然不支持软解但是为开发者扩展软解预留了接口。
网上竟然搜不到ExoPlayer+软解的扩展实现,那就有必要来一篇了hhh。

预先准备:
1.下载NDK(要下载NDK r15c,高版本编译不过)
https://developer.android.com/ndk/downloads?hl=zh-cn
2.下载EXO源码
git clone https://github.com/google/ExoPlayer.git
git checkout release-v2

添加ffmpeg软解扩展:
1.找到这个目录ExoPlayer/extensions/ffmpeg/src/main/jni。
在这个目录下执行如下步骤
1.git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
2.cd ffmpeg && git checkout release/4.0
3.配置ffmpeg编译参数,需要添加具体的某种解码器可自行配置

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

4.根据要编译的架构编写编译脚本

ANDROID_NDK=/Users/hawkswang/ndk/android-ndk-r15c
HOST_PLATFORM=darwin-x86_64
./configure \
    --libdir=android-libs/armeabi-v7a \
    --arch=arm \
    --cpu=armv7-a \
    --cross-prefix="${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/${HOST_PLATFORM}/bin/arm-linux-androideabi-" \
    --sysroot="${ANDROID_NDK}/platforms/android-9/arch-arm/" \
    --extra-cflags="-march=armv7-a -mfloat-abi=softfp" \
    --extra-ldflags="-Wl,--fix-cortex-a8" \
    --extra-ldexeflags=-pie \
    ${COMMON_OPTIONS} \

5.开始编译
make -j4 && make install-libs

执行完步骤5就可以看到在ffmpeg目录下生成了如图.so
[外链图片转存失败(img-wRGeZWlD-1563458349062)(http://km.oa.com/files/photos/pictures//20190326//1553572014_31.png)]

6.编写JNI(如果想要扩展更多功能的话)

7.交叉编译JNI接口
在目录下ExoPlayer/extensions/ffmpeg/src/main/jni
执行

ANDROID_NDK=/Users/hawkswang/ndk/android-ndk-r15c
HOST_PLATFORM=darwin-x86_64
${ANDROID_NDK}/ndk-build APP_ABI="armeabi armeabi-v7a arm64-v8a" -j4

[外链图片转存失败(img-lhDICtUo-1563458349065)(http://km.oa.com/files/photos/pictures//20190326//1553572387_41.png)]
执行完7步骤即会在出现如图.so

8.在Java层初始化播放器的时候传人EXTENSION_RENDERER_MODE_PREFER即使用扩展的软解进行解码。
[外链图片转存失败(img-phT5JiH0-1563458349065)(http://km.oa.com/files/photos/pictures//20190326//1553580223_93.png)]

下面是把第1~5部整合到一块的脚本;

#!/bin/bash
ANDROID_NDK=/Users/hawkswang/ndk/android-ndk-r15c
HOST_PLATFORM=darwin-x86_64
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-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 \
    " && \
#(git -C ffmpeg pull || git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg) && \
cd ffmpeg && git checkout release/4.0 && \
./configure \
    --libdir=android-libs/armeabi-v7a \
    --arch=arm \
    --cpu=armv7-a \
    --cross-prefix="${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/${HOST_PLATFORM}/bin/arm-linux-androideabi-" \
    --sysroot="${ANDROID_NDK}/platforms/android-9/arch-arm/" \
    --extra-cflags="-march=armv7-a -mfloat-abi=softfp" \
    --extra-ldflags="-Wl,--fix-cortex-a8" \
    --extra-ldexeflags=-pie \
    ${COMMON_OPTIONS} \
    && \
make -j4 && make install-libs && \
make clean && ./configure \
    --libdir=android-libs/arm64-v8a \
    --arch=aarch64 \
    --cpu=armv8-a \
    --cross-prefix="${ANDROID_NDK}/toolchains/aarch64-linux-android-4.9/prebuilt/${HOST_PLATFORM}/bin/aarch64-linux-android-" \
    --sysroot="${ANDROID_NDK}/platforms/android-21/arch-arm64/" \
    --extra-ldexeflags=-pie \
    ${COMMON_OPTIONS} \
    && \
make -j4 && make install-libs && \
make clean && ./configure \
    --libdir=android-libs/armeabi \
    --arch=arm \
    --cpu=armv5 \
    --cross-prefix="${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/${HOST_PLATFORM}/bin/arm-linux-androideabi-" \
    --sysroot="${ANDROID_NDK}/platforms/android-9/arch-arm/" \
    --extra-cflags="-march=armv5 -mfloat-abi=softfp" \
    --extra-ldflags="-Wl,--fix-cortex-a8" \
    --extra-ldexeflags=-pie \
    ${COMMON_OPTIONS} \
    && \
make -j4 && make install-libs && \
make clean

echo "Success !"

你可能感兴趣的:(Android音视频)