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

/ 3,128评论 / 23531阅读 / 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. profi-teh-remont说道:

    Сервисный центр предлагает ремонт panasonic nv-ds11 в петербурге ремонт panasonic nv-ds11

  2. Jefferyswava说道:

    mexico drug stores pharmacies: mexicanpharmgate.com – п»їbest mexican online pharmacies

  3. This is a topic close to my heart cheers, where are your contact details though?

  4. Jamiescact说道:

    https://mexicanpharmgate.com/# mexican drugstore online

  5. Jamiescact说道:

    https://canadiandrugsgate.com/# ed problems treatment

  6. Тут можно преобрести шкаф для оружия сейф для оружия купить в москве

  7. Тут можно преобрести огнеупорный сейф купить сейф несгораемый

  8. LarryHer说道:

    the best ed pills: canadian pharmacy – ed pills that work quickly

  9. Jefferyswava说道:

    natural ed medications: Best Canadian pharmacy – comparison of ed drugs

  10. Тут можно преобрести купить сейф оружейный в москве оружейные сейфы цена

  11. Jamiescact说道:

    http://mexicanpharmgate.com/# mexico drug stores pharmacies

  12. Jamiescact说道:

    http://indianpharmacyeasy.com/# п»їlegitimate online pharmacies india

  13. Тут можно преобрести купить несгораемый сейф купить сейф несгораемый

  14. LarryHer说道:

    natural cure for ed: Canadian pharmacy best prices – ed medicine online

  15. Bradleyarrip说道:

    http://indianpharmacyeasy.com/# india pharmacy mail order

  16. Jefferyswava说道:

    medication from mexico pharmacy: mexican pharmacy online medications – mexico drug stores pharmacies

  17. Тут можно преобрести купить огнестойкий сейф в москве несгораемый сейф

  18. Jamiescact说道:

    http://canadiandrugsgate.com/# online meds for ed

  19. Jamiescact说道:

    http://mexicanpharmgate.com/# buying prescription drugs in mexico online

  20. Тут можно преобрести охотничьи сейфы оружейные сейфы купить

  21. Jamiescact说道:

    http://mexicanpharmgate.com/# mexican pharmaceuticals online

  22. Jefferyswava说道:

    reputable indian online pharmacy: indianpharmacyeasy.com – india pharmacy mail order

  23. Тут можно преобрести оружейные сейфы шкафы шкаф для оружия цена

  24. Тут можно преобрести сейф несгораемый сейф противопожарный

  25. Тут можно преобрести цена сейфа для оружия сейф для пистолета и ружья

  26. Тут можно преобрести купить сейф огнестойкий в москве сейф противопожарный

  27. profi-teh-remont说道:

    Сервисный центр предлагает ремонт acer timelinex 3830t ремонт acer timelinex 3830t

  28. RobertCaw说道:

    kamagra oral jelly: kam pharm shop – kamagra oral jelly

  29. RobertCaw说道:

    cheapest Kamagra Kam Pharm: kamagra oral jelly – kampharm.shop

  30. DennisBob说道:

    gabapentin GabaPharm: cheapest Gabapentin GabaPharm – buy gabapentin india

  31. CurtisTraup说道:

    semaglutide buy rybelsus rybpharm semaglutide

  32. Тут можно преобрести огнестойкие сейфы купить сейф несгораемый

  33. Great paintings! That is the type of information that should be shared across the net. Disgrace on the seek engines for no longer positioning this post higher! Come on over and visit my website . Thanks =)

  34. DennisBob说道:

    buy ed pills: ED meds online with insurance – erepharm pills

  35. CurtisTraup说道:

    buy lasix fur pharm furosemide fur pharm cheapest lasix

  36. DennisBob说道:

    furosemide furpharm.com: lasix – buy furosemide online

  37. CurtisTraup说道:

    rybpharm rybpharm rybelsus rybpharm rybelsus

  38. Lloydlania说道:

    https://furpharm.com/# cheapest lasix

  39. Rileyaromi说道:

    Viagra * Cialis * Levitra

    All the products you are looking for are currently close by in support of 1+1.

    4 more tablets of one of the following services: Viagra * Cialis * Levitra

    https://pxman.net

  40. Robertmep说道:

    https://gabapharm.com/# cheapest Gabapentin GabaPharm

  41. Robertmep说道:

    https://kampharm.shop/# cheapest Kamagra Kam Pharm

  42. Тут можно преобрести сейф оружейный доставка сейфы под оружие

  43. DennisBob说道:

    cheapest Kamagra Kam Pharm: cheapest Kamagra Kam Pharm – buy kamagra oral jelly Kam Pharm

  44. CurtisTraup说道:

    erepharm pills buy ed pills ED meds online with insurance

  45. DennisBob说道:

    buy gabapentin online: Buy gabapentin for humans – gabapentin GabaPharm

发表回复

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