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

/ 2,979评论 / 22877阅读 / 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. JesusDrems说道:

    sweet bonanza slot demo: sweet bonanza demo turkce – slot oyunlari

  2. JesusDrems说道:

    yeni slot siteleri: oyun siteleri slot – yasal slot siteleri

  3. JesusDrems说道:

    sweet bonanza free spin demo: sweet bonanza free spin demo – pragmatic play sweet bonanza

  4. KevinOvems说道:

    http://slotsiteleri.bid/# en cok kazandiran slot siteleri

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

  6. KevinOvems说道:

    https://sweetbonanza.network/# sweet bonanza oyna

  7. DarrelTrini说道:

    oyun siteleri slot: en yeni slot siteleri – en cok kazandiran slot siteleri

  8. DarrelTrini说道:

    slot kumar siteleri: slot siteleri guvenilir – slot kumar siteleri

  9. DarrelTrini说道:

    en guvenilir slot siteleri: slot siteleri bonus veren – bonus veren slot siteleri

  10. Techno rozen说道:

    Real Estate I just like the helpful information you provide in your articles

  11. DarrelTrini说道:

    oyun siteleri slot: en cok kazandiran slot siteleri – slot siteleri 2024

  12. JesusDrems说道:

    sweet bonanza siteleri: sweet bonanza slot – sweet bonanza free spin demo

  13. JesusDrems说道:

    sweet bonanza demo: sweet bonanza free spin demo – sweet bonanza slot demo

  14. KevinOvems说道:

    https://slotsiteleri.bid/# deneme bonusu veren siteler

  15. JesusDrems说道:

    sweet bonanza yorumlar: sweet bonanza free spin demo – sweet bonanza mostbet

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

  17. DarrelTrini说道:

    slot oyun siteleri: en iyi slot siteler – 2024 en iyi slot siteleri

  18. DarrelTrini说道:

    slot siteleri guvenilir: slot kumar siteleri – yeni slot siteleri

  19. JesusDrems说道:

    deneme veren slot siteleri: slot siteleri bonus veren – canl? slot siteleri

  20. DarrelTrini说道:

    casino slot siteleri: bonus veren casino slot siteleri – slot siteleri 2024

  21. HermanKak说道:

    cheap canadian pharmacy online: buy canadian drugs – canada drugstore pharmacy rx

  22. HermanKak说道:

    indian pharmacy: indian pharmacies safe – cheapest online pharmacy india

  23. PeterFum说道:

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

  24. lien real estate说道:

    Real Estate I appreciate you sharing this blog post. Thanks Again. Cool.

  25. PeterFum说道:

    Online medicine order: indian pharmacies safe – Online medicine home delivery

  26. Real Estate naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  27. PeterFum说道:

    reputable indian pharmacies: world pharmacy india – Online medicine home delivery

  28. JeremyRiz说道:

    http://easyrxcanada.com/# online canadian pharmacy reviews

  29. JeremyRiz说道:

    http://easyrxcanada.com/# best canadian pharmacy

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

  31. Robertkaw说道:

    http://easyrxcanada.com/# cheap canadian pharmacy

  32. Robertkaw说道:

    https://mexstarpharma.online/# mexican border pharmacies shipping to usa

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

  34. HermanKak说道:

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

  35. JeremyRiz说道:

    http://easyrxcanada.com/# canadian pharmacies comparison

  36. JeremyRiz说道:

    https://mexstarpharma.com/# pharmacies in mexico that ship to usa

  37. Robertkaw说道:

    http://easyrxcanada.com/# northern pharmacy canada

  38. HermanKak说道:

    cheapest online pharmacy india: world pharmacy india – indian pharmacy

  39. Robertkaw说道:

    https://easyrxcanada.online/# canadapharmacyonline legit

  40. PeterFum说道:

    canadian valley pharmacy: canadian 24 hour pharmacy – reliable canadian pharmacy

  41. PeterFum说道:

    canadapharmacyonline legit: my canadian pharmacy rx – canadian pharmacy no rx needed

  42. PeterFum说道:

    mexican online pharmacies prescription drugs: buying from online mexican pharmacy – reputable mexican pharmacies online

  43. HermanKak说道:

    canadian online pharmacy: canadian online pharmacy – canadian pharmacy world

  44. HermanKak说道:

    mexican online pharmacies prescription drugs: mexican mail order pharmacies – mexican pharmacy

  45. JeremyRiz说道:

    http://easyrxcanada.com/# canadian pharmacy store

  46. JeremyRiz说道:

    https://easyrxindia.com/# best india pharmacy

  47. Robertkaw说道:

    http://easyrxcanada.com/# precription drugs from canada

  48. Robertkaw说道:

    http://mexstarpharma.com/# mexican mail order pharmacies

发表回复

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