[安卓/flutter]给Media3.exoplayer编译扩展ffmpeg解码器

/ 2,884评论 / 22425阅读 / 2点赞

前言

引用文档

准备

配置NDK 、cmdtools、SDKManager

cd {你想放NDK的目录}
wget {官网复制Linux版的下载链接}
unzip {刚刚下载的zip文件}
cd {cmdline-tools目录}/bin
sudo ./sdkmanager --licenses --sdk_root={你的NDK的根目录}

编译ffmpge

NDK_PATH="$(pwd)"
cd {你想放Media3.decoder_ffmpeg的目录}
git clone https://github.com/androidx/media --depth 1
cd media
FFMPEG_MODULE_PATH="$(pwd)/libraries/decoder_ffmpeg/src/main"
Lunux执行:
HOST_PLATFORM="linux-x86_64"

MacOX执行:
HOST_PLATFORM="darwin-x86_64"
ANDROID_ABI=21
cd {你想放ffmpge源码的目录}
git clone git://source.ffmpeg.org/ffmpeg && \ cd ffmpeg && \ git checkout release/6.0 && \ FFMPEG_PATH="$(pwd)"
ENABLED_DECODERS=(vorbis opus flac alac pcm_mulaw pcm_alaw mp3 amrnb amrwb aac ac3 eac3 dca mlp truehd)
cd "${FFMPEG_MODULE_PATH}/jni"
ln -s "$FFMPEG_PATH" ffmpeg
./build_ffmpeg.sh \ "${FFMPEG_MODULE_PATH}" "${NDK_PATH}" "${HOST_PLATFORM}" "${ANDROID_ABI}" "${ENABLED_DECODERS[@]}"
cd {开头放Media3.decoder_ffmpeg的目录}
./gradlew lib-decoder-ffmpeg:assembleRelease

项目引入ffmpge的aar包


dependencies {
    implementation(fileTree('libs'))
}
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

// 把aar拷贝进入主项目的方法 com.example.android_control换成你自己的插件包名
// * 只修改下面这一行的参数就可以了
copyAar2Host('com.example.android_control')

// 拷贝aar的方法
static aarFileCopy(String srcPath,String desPath) {
    System.out.println("copy aar from <<${srcPath}>> to <<${desPath}>>")
    try {
        FileInputStream fis = new FileInputStream(srcPath)
        FileOutputStream fos = new FileOutputStream(desPath)
        byte[] data = new byte[1024*8]
        int len = 0
        while ((len = fis.read(data))!=-1) {
            fos.write(data,0,len)
        }
        fis.close()
        fos.close()
    }catch(Exception e) {
        e.printStackTrace()
    }
}

void copyAar2Host(String pluginGroup) {
    Project currentProject = null
    Project appProject = null
    rootProject.allprojects.each {
        p->
            boolean  isApp = p.plugins.hasPlugin("com.android.application")
            println("<<${p.name}>> isHost ? ${isApp}")
            if (p.group == pluginGroup) {
                currentProject = p
                println("Plugin project name is $currentProject")
            }
            if(isApp) {
                appProject = p
                println("Host project name is <<${p.name}>>")
            }
    }
    Set<File> aarFiles = new HashSet<File>()
    if (appProject != null && currentProject != null) {
        File libs = new File("${currentProject.projectDir}","libs")
        if(libs.isDirectory()) {
            libs.listFiles().each {
                f->
                    if(f.name.endsWith(".aar")) {
                        println("The aar file name to be copied is <<${f.name}>>")
                        aarFiles.add(f)
                    }

            }
        }
        if (!aarFiles.isEmpty()) {
            File applibs = new File("${appProject.projectDir}${File.separator}libs")
            if(!applibs.isDirectory()) {
                applibs.mkdirs()
            }
            aarFiles.each {
                f->
                    File copyAar = new File("${appProject.projectDir}${File.separator}libs",f.name)
                    if(!copyAar.exists()) {
                        copyAar.createNewFile()
                        aarFileCopy(f.path,copyAar.path)
                    } else {

                    }
            }
            appProject.dependencies {
                implementation fileTree(dir:"${appProject.projectDir}${File.separator}libs",include:["*.jar","*.aar"])
            }
        }
    }
}

repositories{
    flatDir {
        dirs 'libs'
    }
}
apply plugin: 'com.android.library'
// 在这里插入下面这一行
apply from: './aar_tools.gradle'

// 然后找到dependencies块,
dependencies {
    // 这一行导入刚刚libs目录内的ffmpge的arr包

    compileOnly(fileTree("libs"))
    // 这里用于解决用android studio打开项目的android目录时里面的java/kotlin文件的import都是灰色的问题
    compileOnly files("$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar")
    compileOnly("androidx.annotation:annotation:1.3.0")
}

在代码里引入ffmpeg


import androidx.media3.decoder.ffmpeg.FfmpegAudioRenderer;

class FfmpegRenderersFactory extends DefaultRenderersFactory {
    public FfmpegRenderersFactory(Context context) {
        super(context);
        setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON);
    }

    @Override
    protected void buildAudioRenderers(Context context, int extensionRendererMode, MediaCodecSelector mediaCodecSelector, boolean enableDecoderFallback, AudioSink audioSink, Handler eventHandler, AudioRendererEventListener eventListener, ArrayList<Renderer> out) {
        out.add(new FfmpegAudioRenderer(eventHandler, eventListener, audioSink));
        super.buildAudioRenderers(context, extensionRendererMode, mediaCodecSelector, enableDecoderFallback, audioSink, eventHandler, eventListener, out);
    }
}

ExoPlayer.Builder builder = new ExoPlayer.Builder(context);
// 添加ffmpeg

builder.setRenderersFactory(new FfmpegRenderersFactory(context));
ExoPlayer player = builder.build();
  1. NelsonSaIlk说道:

    mexican mail order pharmacies: mexico drug stores pharmacies – medication from mexico pharmacy

  2. celebio说道:

    you are in reality a good webmaster The website loading velocity is amazing It sort of feels that youre doing any distinctive trick Also The contents are masterwork you have done a fantastic job in this topic

  3. DominicHOr说道:

    buying prescription drugs in mexico online: mexican pharmaceuticals online – mexican mail order pharmacies

  4. DominicHOr说道:

    п»їbest mexican online pharmacies: mexican online pharmacies prescription drugs – mexico drug stores pharmacies

  5. celebio说道:

    Wow amazing blog layout How long have you been blogging for you made blogging look easy The overall look of your web site is magnificent as well as the content

  6. Arnoldscava说道:

    medicine in mexico pharmacies medicine in mexico pharmacies buying prescription drugs in mexico online

  7. Waynetar说道:

    mexican pharmaceuticals online: mexican mail order pharmacies – mexican mail order pharmacies

  8. Waynetar说道:

    best online pharmacies in mexico: buying prescription drugs in mexico online – mexican mail order pharmacies

  9. Arnoldscava说道:

    buying prescription drugs in mexico medication from mexico pharmacy mexico drug stores pharmacies

  10. webech说道:

    Hello my loved one I want to say that this post is amazing great written and include almost all significant infos I would like to look extra posts like this

  11. NelsonSaIlk说道:

    reputable mexican pharmacies online: mexico drug stores pharmacies – mexican pharmaceuticals online

  12. Arnoldscava说道:

    mexican pharmaceuticals online mexican drugstore online medicine in mexico pharmacies

  13. NelsonSaIlk说道:

    buying prescription drugs in mexico: mexico pharmacies prescription drugs – mexico drug stores pharmacies

  14. DominicHOr说道:

    mexico drug stores pharmacies: mexico pharmacies prescription drugs – mexican online pharmacies prescription drugs

  15. DominicHOr说道:

    best online pharmacies in mexico: п»їbest mexican online pharmacies – purple pharmacy mexico price list

  16. Waynetar说道:

    purple pharmacy mexico price list: best online pharmacies in mexico – buying prescription drugs in mexico

  17. Waynetar说道:

    mexican mail order pharmacies: mexico drug stores pharmacies – mexican rx online

  18. Arnoldscava说道:

    mexico pharmacy medication from mexico pharmacy best online pharmacies in mexico

  19. NelsonSaIlk说道:

    mexican mail order pharmacies: medicine in mexico pharmacies – medication from mexico pharmacy

  20. DominicHOr说道:

    purple pharmacy mexico price list: medicine in mexico pharmacies – best online pharmacies in mexico

  21. NelsonSaIlk说道:

    mexico drug stores pharmacies: п»їbest mexican online pharmacies – mexican mail order pharmacies

  22. DominicHOr说道:

    mexican online pharmacies prescription drugs: best online pharmacies in mexico – mexican mail order pharmacies

  23. Arnoldscava说道:

    reputable mexican pharmacies online purple pharmacy mexico price list medicine in mexico pharmacies

  24. Waynetar说道:

    reputable mexican pharmacies online: buying from online mexican pharmacy – mexican border pharmacies shipping to usa

  25. Arnoldscava说道:

    mexican mail order pharmacies reputable mexican pharmacies online buying prescription drugs in mexico online

  26. Waynetar说道:

    buying prescription drugs in mexico: pharmacies in mexico that ship to usa – mexican pharmaceuticals online

  27. Arnoldscava说道:

    buying prescription drugs in mexico mexican online pharmacies prescription drugs mexican border pharmacies shipping to usa

  28. DominicHOr说道:

    mexico pharmacies prescription drugs: medicine in mexico pharmacies – buying prescription drugs in mexico

  29. DominicHOr说道:

    buying prescription drugs in mexico: buying from online mexican pharmacy – medication from mexico pharmacy

  30. NelsonSaIlk说道:

    п»їbest mexican online pharmacies: mexican pharmaceuticals online – buying prescription drugs in mexico online

  31. NelsonSaIlk说道:

    mexican mail order pharmacies: mexico drug stores pharmacies – medication from mexico pharmacy

  32. Waynetar说道:

    mexican drugstore online: mexican online pharmacies prescription drugs – mexican drugstore online

  33. Waynetar说道:

    mexican pharmaceuticals online: mexico pharmacies prescription drugs – mexico pharmacies prescription drugs

  34. Arnoldscava说道:

    mexican pharmaceuticals online pharmacies in mexico that ship to usa mexican online pharmacies prescription drugs

  35. DominicHOr说道:

    purple pharmacy mexico price list: mexican online pharmacies prescription drugs – buying prescription drugs in mexico

  36. DominicHOr说道:

    mexican border pharmacies shipping to usa: mexican pharmaceuticals online – buying from online mexican pharmacy

  37. NelsonSaIlk说道:

    mexican rx online: medicine in mexico pharmacies – п»їbest mexican online pharmacies

  38. NelsonSaIlk说道:

    medication from mexico pharmacy: purple pharmacy mexico price list – medication from mexico pharmacy

  39. Arnoldscava说道:

    mexican pharmacy mexican pharmacy mexican pharmacy

  40. Arnoldscava说道:

    buying prescription drugs in mexico mexico pharmacies prescription drugs reputable mexican pharmacies online

  41. Waynetar说道:

    mexican mail order pharmacies: mexican rx online – п»їbest mexican online pharmacies

  42. Waynetar说道:

    mexican border pharmacies shipping to usa: best online pharmacies in mexico – mexico drug stores pharmacies

  43. Arnoldscava说道:

    purple pharmacy mexico price list medication from mexico pharmacy reputable mexican pharmacies online

  44. DominicHOr说道:

    mexican drugstore online: purple pharmacy mexico price list – п»їbest mexican online pharmacies

  45. DominicHOr说道:

    medication from mexico pharmacy: mexico drug stores pharmacies – п»їbest mexican online pharmacies

  46. NelsonSaIlk说道:

    medicine in mexico pharmacies: reputable mexican pharmacies online – mexican rx online

  47. NelsonSaIlk说道:

    п»їbest mexican online pharmacies: mexico drug stores pharmacies – mexican border pharmacies shipping to usa

  48. Waynetar说道:

    buying prescription drugs in mexico: buying prescription drugs in mexico – mexican online pharmacies prescription drugs

  49. latestsession说道:

    Wow wonderful blog layout How long have you been blogging for you make blogging look easy The overall look of your site is great as well as the content

  50. DominicHOr说道:

    buying from online mexican pharmacy: п»їbest mexican online pharmacies – reputable mexican pharmacies online

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注