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

/ 2,992评论 / 22957阅读 / 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. MauriceSig说道:

    top 10 online pharmacy in india: indian pharmacy online – indian pharmacy paypal

  2. Robertten说道:

    mexican rx online: mexican border pharmacies shipping to usa – pharmacies in mexico that ship to usa

  3. Robertten说道:

    metoclopramide online pharmacy: provigil online pharmacy – diamox online pharmacy

  4. MauriceSig说道:

    mexican rx online: mexican border pharmacies shipping to usa – medication from mexico pharmacy

  5. JosephTycle说道:

    pharmacies in mexico that ship to usa buying prescription drugs in mexico mexican border pharmacies shipping to usa

  6. Robertten说道:

    mexican drugstore online: mexico pharmacies prescription drugs – best online pharmacies in mexico

  7. JosephTycle说道:

    medication from mexico pharmacy purple pharmacy mexico price list mexican mail order pharmacies

  8. JosephTycle说道:

    purple pharmacy mexico price list mexican rx online mexican border pharmacies shipping to usa

  9. Robertten说道:

    mexican mail order pharmacies: mexican mail order pharmacies – pharmacies in mexico that ship to usa

  10. Arthurcig说道:

    http://indianpharmacy.company/# п»їlegitimate online pharmacies india

  11. Arthurcig说道:

    http://mexicopharmacy.cheap/# mexican drugstore online

  12. Arthurcig说道:

    http://pharmbig24.com/# singulair pharmacy coupon

  13. Arthurcig说道:

    https://indianpharmacy.company/# buy prescription drugs from india

  14. Robertten说道:

    prevacid pharmacy coupons: target pharmacy finasteride – AebgMaync

  15. Robertten说道:

    india pharmacy mail order: reputable indian pharmacies – buy medicines online in india

  16. JosephTycle说道:

    indian pharmacies safe best online pharmacy india reputable indian pharmacies

  17. MauriceSig说道:

    mexican rx online: mexican border pharmacies shipping to usa – medication from mexico pharmacy

  18. Robertten说道:

    india pharmacy mail order: online shopping pharmacy india – pharmacy website india

  19. JosephTycle说道:

    п»їlegitimate online pharmacies india indian pharmacies safe world pharmacy india

  20. JosephTycle说道:

    indian pharmacies safe online pharmacy india indian pharmacies safe

  21. Robertten说道:

    п»їbest mexican online pharmacies: medicine in mexico pharmacies – buying from online mexican pharmacy

  22. MauriceSig说道:

    u s a online pharmacy: zyprexa pharmacy online – hq pharmacy online 365

  23. Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
    Мы предлагаем: ремонт бытовой техники в казани
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  24. Профессиональный сервисный центр по ремонту стиральных машин с выездом на дом по Москве.
    Мы предлагаем: ремонт стиральных машин в москве недорого
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  25. MauriceSig说道:

    mexican rx online: medication from mexico pharmacy – buying from online mexican pharmacy

  26. MauriceSig说道:

    rx pharmacy store: Indinavir (Cipla Ltd) – lamotrigine pharmacy

  27. Arthurcig说道:

    https://mexicopharmacy.cheap/# mexican mail order pharmacies

  28. Robertten说道:

    cheap online pharmacy: Claritin – spironolactone inhouse pharmacy

  29. Robertten说道:

    п»їlegitimate online pharmacies india: best india pharmacy – cheapest online pharmacy india

  30. JosephTycle说道:

    mexican mail order pharmacies mexican drugstore online п»їbest mexican online pharmacies

  31. Robertten说道:

    get cialis online pharmacy: canadia online pharmacy – atacand online pharmacy

  32. Arthurcig说道:

    http://mexicopharmacy.cheap/# best online pharmacies in mexico

  33. Robertten说道:

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

  34. Arthurcig说道:

    https://pharmbig24.online/# online pharmacy painkillers

  35. JosephTycle说道:

    mail order pharmacy india indian pharmacy top 10 online pharmacy in india

  36. MauriceSig说道:

    cabergoline pharmacy: lisinopril publix pharmacy – misoprostol malaysia pharmacy

  37. Если вы искали где отремонтировать сломаную технику, обратите внимание – профи услуги

  38. MauriceSig说道:

    best india pharmacy: indian pharmacy – Online medicine home delivery

  39. MauriceSig说道:

    medicine in mexico pharmacies: pharmacies in mexico that ship to usa – medication from mexico pharmacy

  40. Robertten说道:

    online pharmacy no prescription ultram: save on pharmacy – flovent online pharmacy

  41. Robertten说道:

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

  42. Robertten说道:

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

  43. JosephTycle说道:

    best india pharmacy top 10 online pharmacy in india india online pharmacy

  44. Robertten说道:

    reputable indian online pharmacy: reputable indian online pharmacy – world pharmacy india

  45. JosephTycle说道:

    india pharmacy mail order world pharmacy india india pharmacy mail order

  46. Arthurcig说道:

    https://mexicopharmacy.cheap/# medication from mexico pharmacy

  47. JosephTycle说道:

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

  48. I was looking through some of your content on this internet site and I think this internet site is really instructive! Keep putting up.

  49. Arthurcig说道:

    https://pharmbig24.com/# boots online pharmacy doxycycline

发表回复

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