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

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

    order cytotec online https://cytotec.pro/# buy cytotec pills online cheap
    lasix for sale

  2. La weekly说道:

    La weekly I just like the helpful information you provide in your articles

  3. DanielDrync说道:

    buy cytotec pills https://tamoxifen.bid/# tamoxifen and grapefruit
    lasix dosage

  4. JamesRok说道:

    lipitor brand price: cheapest ace inhibitor – cost of lipitor in canada

  5. JamesRok说道:

    buy cytotec pills: cytotec best price – Abortion pills online

  6. JamesRok说道:

    furosemida: lasix dosage – lasix

  7. Ny weekly说道:

    Ny weekly I am truly thankful to the owner of this web site who has shared this fantastic piece of writing at at this place.

  8. JamesRok说道:

    clomid nolvadex: Purchase Nolvadex Online – tamoxifen medication

  9. JamesRok说道:

    п»їbuy lisinopril 10 mg uk: Buy Lisinopril 20 mg online – lisinopril 20 mg tablets

  10. JamesRok说道:

    buy cytotec pills online cheap: purchase cytotec – cytotec pills buy online

  11. DanielDrync说道:

    buy cytotec pills https://lisinopril.guru/# lisinopril 2019
    furosemide 100 mg

  12. DanielDrync说道:

    Abortion pills online http://furosemide.win/# lasix furosemide 40 mg
    furosemide 100mg

  13. DanielDrync说道:

    buy misoprostol over the counter http://cytotec.pro/# cytotec pills buy online
    furosemide 40 mg

  14. DanielDrync说道:

    buy misoprostol over the counter https://lipitor.guru/# lipitor 80 mg daily
    furosemide

  15. JamesRok说道:

    average cost of generic lipitor: buy atorvastatin online – lipitor atorvastatin calcium

  16. JamesRok说道:

    buy cytotec pills online cheap: cytotec best price – buy cytotec online fast delivery

  17. JamesRok说道:

    lasix 40 mg: buy furosemide – generic lasix

  18. JamesRok说道:

    lipitor 10 mg: liptor – buy brand lipitor

  19. JamesRok说道:

    pfizer lipitor: cheapest ace inhibitor – generic lipitor 10 mg

  20. DanielDrync说道:

    п»їcytotec pills online http://furosemide.win/# buy lasix online
    furosemide 40 mg

  21. DanielDrync说道:

    buy cytotec online http://cytotec.pro/# buy cytotec
    lasix tablet

  22. DanielDrync说道:

    buy cytotec online http://lipitor.guru/# lipitor generic brand
    furosemide 40 mg

  23. DanielDrync说道:

    cytotec buy online usa https://furosemide.win/# lasix generic
    furosemide

  24. JamesRok说道:

    tamoxifen effectiveness: buy tamoxifen citrate – tamoxifen adverse effects

  25. JamesRok说道:

    buy lipitor online uk: Lipitor 10 mg price – lipitor 100mg

  26. JamesRok说道:

    lisinopril 102: buy lisinopril – price of lisinopril 20 mg

  27. JamesRok说道:

    lipitor for sale: cost of lipitor – buy lipitor 40 mg

  28. DanielDrync说道:

    cytotec abortion pill https://cytotec.pro/# purchase cytotec
    furosemide 100mg

  29. DanielDrync说道:

    order cytotec online http://lisinopril.guru/# lisinopril 7.5 mg
    lasix 40mg

  30. JamesRok说道:

    buy cytotec online fast delivery: cheapest cytotec – cytotec buy online usa

  31. DanielDrync说道:

    п»їcytotec pills online https://furosemide.win/# lasix 100 mg
    generic lasix

  32. StephenJonge说道:

    http://lipitor.guru/# lipitor generic australia

  33. DanielDrync说道:

    buy cytotec pills online cheap http://furosemide.win/# generic lasix
    buy lasix online

  34. StephenJonge说道:

    https://tamoxifen.bid/# tamoxifen for sale

  35. JamesRok说道:

    purchase cytotec: cheapest cytotec – buy cytotec online fast delivery

  36. JamesRok说道:

    Cytotec 200mcg price: cheapest cytotec – cytotec buy online usa

  37. JamesRok说道:

    lisinopril 5 mg buy: lisinopril 4 mg – lisinopril 2.5 mg medicine

  38. DanielDrync说道:

    purchase cytotec https://cytotec.pro/# order cytotec online
    lasix 100mg

  39. DanielDrync说道:

    п»їcytotec pills online https://cytotec.pro/# purchase cytotec
    lasix uses

  40. JamesRok说道:

    generic lipitor online: buy atorvastatin online – lipitor medication

  41. JamesRok说道:

    lisinopril 2019: order lisinopril online united states – lisinopril pills 10 mg

  42. JamesRok说道:

    furosemide 100 mg: furosemide online – lasix dosage

  43. StephenJonge说道:

    http://lipitor.guru/# prescription medication lipitor

  44. StephenJonge说道:

    http://cytotec.pro/# Abortion pills online

  45. StephenJonge说道:

    https://lisinopril.guru/# zestril cost price

  46. StephenJonge说道:

    https://tamoxifen.bid/# tamoxifen and bone density

  47. JamesRok说道:

    lisinopril tabs 88mg: cheap lisinopril – lisinopril buy in canada

发表回复

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