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

/ 2,977评论 / 22868阅读 / 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. PeterFum说道:

    reputable canadian online pharmacies: canadian drugs – my canadian pharmacy

  2. JeremyRiz说道:

    http://easyrxcanada.com/# canadadrugpharmacy com

  3. Robertkaw说道:

    https://mexstarpharma.online/# reputable mexican pharmacies online

  4. thefriskys说道:

    helloI really like your writing so a lot share we keep up a correspondence extra approximately your post on AOL I need an expert in this house to unravel my problem May be that is you Taking a look ahead to see you

  5. PeterFum说道:

    canadian mail order pharmacy: safe canadian pharmacy – pharmacy rx world canada

  6. JeremyRiz说道:

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

  7. HermanKak说道:

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

  8. Robertkaw说道:

    https://mexstarpharma.online/# п»їbest mexican online pharmacies

  9. HermanKak说道:

    best online pharmacies in mexico: mexican mail order pharmacies – mexico pharmacy

  10. AnthonyFUS说道:

    reputable canadian pharmacy best canadian online pharmacy vipps approved canadian online pharmacy

  11. AnthonyFUS说道:

    reputable indian pharmacies п»їlegitimate online pharmacies india indian pharmacies safe

  12. AnthonyFUS说道:

    mexican mail order pharmacies buying prescription drugs in mexico mexico drug stores pharmacies

  13. PeterFum说道:

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

  14. PeterFum说道:

    online pharmacy india: reputable indian online pharmacy – indianpharmacy com

  15. PeterFum说道:

    reliable canadian pharmacy: cheap canadian pharmacy online – safe reliable canadian pharmacy

  16. JeremyRiz说道:

    http://easyrxcanada.com/# legal to buy prescription drugs from canada

  17. JeremyRiz说道:

    https://mexstarpharma.online/# medication from mexico pharmacy

  18. AnthonyFUS说道:

    canadian mail order pharmacy canadian pharmacy reviews canadian discount pharmacy

  19. Robertkaw说道:

    https://mexstarpharma.online/# purple pharmacy mexico price list

  20. Robertkaw说道:

    http://mexstarpharma.com/# purple pharmacy mexico price list

  21. HermanKak说道:

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

  22. Robertkaw说道:

    https://easyrxcanada.online/# buying drugs from canada

  23. JeremyRiz说道:

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

  24. Robertkaw说道:

    http://easyrxcanada.com/# legitimate canadian online pharmacies

  25. HermanKak说道:

    medicine in mexico pharmacies: mexican mail order pharmacies – medicine in mexico pharmacies

  26. JeremyRiz说道:

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

  27. AnthonyFUS说道:

    mexican drugstore online reputable mexican pharmacies online mexico drug stores pharmacies

  28. AnthonyFUS说道:

    indian pharmacy online mail order pharmacy india online shopping pharmacy india

  29. temp-mail说道:

    Your writing has a way of resonating with me on a deep level. I appreciate the honesty and authenticity you bring to every post. Thank you for sharing your journey with us.

  30. HermanKak说道:

    canadian online pharmacy: canada drug pharmacy – canadian pharmacy ratings

  31. Robertkaw说道:

    http://easyrxindia.com/# online shopping pharmacy india

  32. Robertkaw说道:

    http://easyrxindia.com/# indian pharmacies safe

  33. AnthonyFUS说道:

    mexican border pharmacies shipping to usa mexican mail order pharmacies mexican border pharmacies shipping to usa

  34. Robertkaw说道:

    http://mexstarpharma.com/# п»їbest mexican online pharmacies

  35. Robertkaw说道:

    https://easyrxindia.com/# mail order pharmacy india

  36. HermanKak说道:

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

  37. JeremyRiz说道:

    http://easyrxindia.com/# Online medicine order

  38. JeremyRiz说道:

    https://easyrxcanada.online/# cheapest pharmacy canada

  39. JeremyRiz说道:

    http://mexstarpharma.com/# best online pharmacies in mexico

  40. AnthonyFUS说道:

    canadian valley pharmacy buying drugs from canada canada pharmacy online

  41. AnthonyFUS说道:

    mexico pharmacies prescription drugs medication from mexico pharmacy mexican pharmaceuticals online

  42. Fitspresso说道:

    Along with almost everything that appears to be building inside this subject matter, many of your perspectives are relatively exciting. Having said that, I appologize, but I can not give credence to your whole idea, all be it exhilarating none the less. It appears to us that your commentary are not completely justified and in reality you are generally your self not really entirely confident of the point. In any event I did enjoy reading it.

  43. JamesRok说道:

    furosemida 40 mg: cheap lasix – lasix 100 mg tablet

  44. JamesRok说道:

    prinivil: Lisinopril online prescription – buy lisinopril 20 mg no prescription

  45. JamesRok说道:

    buy cytotec: buy cytotec online – buy cytotec online

  46. DanielDrync说道:

    buy cytotec in usa https://tamoxifen.bid/# nolvadex price
    furosemide 100 mg

  47. DanielDrync说道:

    cytotec abortion pill https://lipitor.guru/# generic lipitor cost
    lasix furosemide 40 mg

发表回复

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