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

/ 2,967评论 / 22832阅读 / 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. Douglasnug说道:

    erectile dysfunction pills for sale: ed medication online – best online ed medication

  2. Douglasnug说道:

    mail order pharmacy india: indian pharmacy – indian pharmacies safe

  3. Leonardskarm说道:

    https://edpillpharmacy.store/# buy erectile dysfunction medication

  4. Leonardskarm说道:

    https://indiapharmacy.shop/# Online medicine order

  5. Douglasnug说道:

    purple pharmacy mexico price list: Medicines Mexico – purple pharmacy mexico price list

  6. Douglasnug说道:

    low cost ed meds online: online ed prescription same-day – ed pills

  7. Leonardskarm说道:

    https://mexicopharmacy.win/# reputable mexican pharmacies online

  8. Leonardskarm说道:

    http://edpillpharmacy.store/# pills for ed online

  9. Douglasnug说道:

    cheap ed: Cheap ED pills online – ed meds on line

  10. Douglasnug说道:

    cheapest online pharmacy india: Online pharmacy – indian pharmacy

  11. Leonardskarm说道:

    https://edpillpharmacy.store/# how to get ed pills

  12. Leonardskarm说道:

    https://indiapharmacy.shop/# online pharmacy india

  13. Douglasnug说道:

    medicine in mexico pharmacies: mexico pharmacy win – reputable mexican pharmacies online

  14. Leonardskarm说道:

    http://mexicopharmacy.win/# pharmacies in mexico that ship to usa

  15. Douglasnug说道:

    cheapest online ed treatment: cheap ed pills online – online ed prescription

  16. Douglasnug说道:

    mexican drugstore online: Medicines Mexico – mexico pharmacies prescription drugs

  17. Douglasnug说道:

    reputable mexican pharmacies online: Best pharmacy in Mexico – mexico drug stores pharmacies

  18. largehints说道:

    olá, gosto muito da sua escrita, então compartilhe, mantemos uma correspondência extra sobre sua postagem na AOL. Preciso de um especialista nesta casa para desvendar meu problema. Talvez seja você. Olhando para frente para vê-lo

  19. Leonardskarm说道:

    https://mexicopharmacy.win/# best online pharmacies in mexico

  20. Leonardskarm说道:

    http://mexicopharmacy.win/# mexican mail order pharmacies

  21. Douglasnug说道:

    buying prescription drugs in mexico: mexican pharmacy – mexican rx online

  22. Leonardskarm说道:

    http://indiapharmacy.shop/# world pharmacy india

  23. Douglasnug说道:

    pharmacies in mexico that ship to usa: Best pharmacy in Mexico – mexican online pharmacies prescription drugs

  24. Tonic Greens说道:

    Hello my friend! I wish to say that this post is awesome, nice written and include almost all significant infos. I’d like to see more posts like this.

  25. Douglasnug说道:

    reputable indian pharmacies: Online pharmacy – world pharmacy india

  26. Douglasnug说道:

    indian pharmacy online: Online medicine home delivery – indianpharmacy com

  27. Douglasnug说道:

    buy medicines online in india: Online pharmacy – indian pharmacy

  28. Leonardskarm说道:

    https://indiapharmacy.shop/# indianpharmacy com

  29. Leonardskarm说道:

    http://indiapharmacy.shop/# top online pharmacy india

  30. Leonardskarm说道:

    https://mexicopharmacy.win/# buying from online mexican pharmacy

  31. Douglasnug说道:

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

  32. Douglasnug说道:

    best online ed meds: Cheap ED pills online – erection pills online

  33. Douglasnug说道:

    mexican pharmaceuticals online: Best online Mexican pharmacy – mexico drug stores pharmacies

  34. Your blog has quickly become one of my favorites. Your writing is both insightful and thought-provoking, and I always come away from your posts feeling inspired. Keep up the phenomenal work!

  35. CharlesLem说道:

    http://mexicopharmacy.win/# mexican rx online
    buy erectile dysfunction pills

  36. CharlesLem说道:

    http://edpillpharmacy.store/# cheap ed drugs
    generic ed meds online

  37. CharlesLem说道:

    http://mexicopharmacy.win/# п»їbest mexican online pharmacies
    best online ed treatment

  38. CharlesLem说道:

    https://mexicopharmacy.win/# mexican mail order pharmacies
    best online ed meds

  39. Leonardskarm说道:

    https://mexicopharmacy.win/# mexico drug stores pharmacies

  40. Leonardskarm说道:

    https://edpillpharmacy.store/# cheapest ed treatment

  41. Douglasnug说道:

    п»їlegitimate online pharmacies india: Top mail order pharmacies – п»їlegitimate online pharmacies india

  42. CharlesLem说道:

    https://edpillpharmacy.store/# top rated ed pills
    erection pills online

  43. Leonardskarm说道:

    https://edpillpharmacy.store/# generic ed meds online

  44. CharlesLem说道:

    http://edpillpharmacy.store/# where to get ed pills
    cheap ed drugs

  45. Douglasnug说道:

    mexico pharmacies prescription drugs: Purple pharmacy online ordering – best online pharmacies in mexico

  46. Leonardskarm说道:

    http://edpillpharmacy.store/# erectile dysfunction meds online

  47. CharlesLem说道:

    https://indiapharmacy.shop/# mail order pharmacy india
    buy ed meds

  48. Douglasnug说道:

    buying prescription drugs in mexico online: Certified Mexican pharmacy – medicine in mexico pharmacies

  49. CharlesLem说道:

    https://mexicopharmacy.win/# best online pharmacies in mexico
    cheap ed meds

  50. sliding shelving说道:

    Very interesting information!Perfect just what I was searching for!

发表回复

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