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

/ 3,140评论 / 23600阅读 / 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. Blue Techker说道:

    Blue Techker There is definately a lot to find out about this subject. I like all the points you made

  2. CharlesTug说道:

    rybelsus cost: rybelsus cost – buy rybelsus

  3. Travishit说道:

    Paxlovid over the counter: paxlovid price – п»їpaxlovid

  4. Richardhog说道:

    minocycline 100 mg pills online stromectol 1st shop ivermectin pills canada

  5. ArthurMot说道:

    https://stromectol1st.shop/# buy minocycline 50mg online
    Online medicine order

  6. Trentabila说道:

    https://paxlovid1st.shop/# paxlovid generic
    new treatments for ed

  7. Trentabila说道:

    http://stromectol1st.shop/# ivermectin malaria
    erectile dysfunction cure

  8. Richardhog说道:

    Paxlovid over the counter paxlovid 1st paxlovid price

  9. Travishit说道:

    clopidogrel bisulfate 75 mg: check clopidogrel pro – buy Clopidogrel over the counter

  10. ArthurMot说道:

    https://stromectol1st.shop/# order minocycline 50mg
    best india pharmacy

  11. CharlesTug说道:

    cheap plavix antiplatelet drug: check clopidogrel pro – Clopidogrel 75 MG price

  12. Travishit说道:

    paxlovid cost without insurance: best price on pills – paxlovid buy

  13. Richardhog说道:

    paxlovid generic check this paxlovid india

  14. Trentabila说道:

    https://clopidogrel.pro/# cheap plavix antiplatelet drug
    drugs causing ed

  15. Полезный сервис быстрого загона ссылок сайта в индексация поисковой системы – быстрая индексация ссылок

  16. Richardhog说道:

    ivermectin 1% best price shop ivermectin generic cream

  17. Trentabila说道:

    http://rybelsus.icu/# rybelsus cost
    pumps for ed

  18. Travishit说道:

    generic name for ivermectin: cheapest stromectol – ivermectin 3 mg dose

  19. Сервисный центр предлагает ремонт консолей valve рядом срочный ремонт консолей valve

  20. CharlesTug说道:

    paxlovid pill: paxlovid 1st – buy paxlovid online

  21. Travishit说道:

    antiplatelet drug: plavix price – Plavix generic price

  22. ArthurMot说道:

    http://stromectol1st.shop/# minocin 50 mg for scabies
    Online medicine home delivery

  23. Полезный сервис быстрого загона ссылок сайта в индексация поисковой системы – быстрая индексация ссылок

  24. Trentabila说道:

    https://rybelsus.icu/# good price
    amoxicillin without a doctor’s prescription

  25. CharlesTug说道:

    rybelsus price: Semaglutide pharmacy price – rybelsus price

  26. Richardhog说道:

    buy Clopidogrel over the counter best price on generic п»їplavix generic

  27. Trentabila说道:

    http://rybelsus.icu/# more
    treating ed

  28. Travishit说道:

    Clopidogrel 75 MG price: here – п»їplavix generic

  29. CharlesTug说道:

    Clopidogrel 75 MG price: clopidogrel pills – п»їplavix generic

  30. ArthurMot说道:

    http://stromectol1st.shop/# minocycline antibiotic
    Online medicine order

  31. Richardhog说道:

    rybelsus price cheaper Semaglutide pharmacy price

  32. Trentabila说道:

    https://rybelsus.icu/# rybelsus price
    drugs online

  33. Travishit说道:

    п»їplavix generic: generic pills – Cost of Plavix on Medicare

  34. Trentabila说道:

    https://paxlovid1st.shop/# paxlovid india
    medication for ed dysfunction

  35. Richardhog说道:

    stromectol pill price stromectol ivermectin buy ivermectin 10 mg

  36. ArthurMot说道:

    http://stromectol1st.shop/# ivermectin oral solution
    buy medicines online in india

  37. Travishit说道:

    paxlovid covid: best price on pills – paxlovid covid

  38. CharlesTug说道:

    buy plavix: buy clopidogrel bisulfate – Cost of Plavix on Medicare

  39. Richardhog说道:

    clopidogrel bisulfate 75 mg here cheap plavix antiplatelet drug

  40. Trentabila说道:

    https://stromectol1st.shop/# topical ivermectin cost
    ed treatments that really work

  41. Travishit说道:

    buy paxlovid online: shop – Paxlovid buy online

  42. Richardhog说道:

    minocycline efectos secundarios stromectol fast delivery minocycline 100mg without doctor

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

  44. Travishit说道:

    Plavix 75 mg price: best price on generic – antiplatelet drug

  45. ArthurMot说道:

    https://stromectol1st.shop/# ivermectin ireland
    buy prescription drugs from india

  46. CharlesTug说道:

    Cost of Plavix without insurance: clopidogrel pills – clopidogrel bisulfate 75 mg

发表回复

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