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

/ 3,125评论 / 23519阅读 / 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. hdhub4uin说道:

    Thank you for the auspicious writeup It in fact was a amusement account it Look advanced to far added agreeable from you However how can we communicate

  2. Brandenven说道:

    prednisone 2.5 mg cost: prednisone ray pharm – steroids prednisone for sale

  3. DanielArill说道:

    amoxicillin in india http://prednisoneraypharm.com/# 2.5 mg prednisone daily

  4. Derekjup说道:

    prednisone 20mg: prednisone ray pharm – prednisone 5mg over the counter

  5. Derekjup说道:

    get clomid online: clomid purchase online rex pharm – where to buy cheap clomid without insurance

  6. Brandenven说道:

    where to buy amoxicillin over the counter: Amoxicillin for sale – cost of amoxicillin

  7. ShaunLolve说道:

    ambien without a doctor’s prescription https://canadiandrugsgate.com/# ed meds online without doctor prescription
    ed meds pills drugs

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

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

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

  11. Jefferyswava说道:

    buy generic ed pills online: Canadian pharmacy online – otc ed drugs

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

  13. Тут можно преобрести сейф оружейный доставка купить сейф для охотничьего ружья

  14. Jefferyswava说道:

    reputable indian pharmacies: indianpharmacyeasy – best india pharmacy

  15. GlennSeade说道:

    ed clinics Best Canadian pharmacy best pills for ed

  16. GlennSeade说道:

    natural drugs for ed Canadian pharmacy online pumps for ed

  17. Bradleyarrip说道:

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

  18. LarryHer说道:

    ed drugs over the counter: canadian drugs gate – ed pharmacy

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

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

  21. Jefferyswava说道:

    help with ed: Canadian pharmacy online – prescription without a doctor’s prescription

  22. Bradleyarrip说道:

    http://indianpharmacyeasy.com/# pharmacy website india

  23. сейф说道:

    Здесь можно преобрести стоимость сейфа сейф цена

  24. Jefferyswava说道:

    indianpharmacy com: indianpharmacyeasy – buy medicines online in india

  25. Здесь можно преобрести купить сейф сейф

  26. LarryHer说道:

    Online medicine home delivery: Best online Indian pharmacy – reputable indian pharmacies

  27. GlennSeade说道:

    cheap erectile dysfunction canadiandrugsgate ed remedies

  28. Bradleyarrip说道:

    https://indianpharmacyeasy.com/# pharmacy website india

  29. Jefferyswava说道:

    mexico drug stores pharmacies: mexican pharmacy online medications – buying prescription drugs in mexico online

  30. GlennSeade说道:

    do i have ed canadian pharmacy drugs gate non prescription erection pills

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

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

  33. Тут можно преобрести охотничьи сейфы сейф для оружия цена купить

  34. Jefferyswava说道:

    buying prescription drugs in mexico: Mexican Pharm Gate – buying prescription drugs in mexico online

  35. Jamiescact说道:

    https://indianpharmacyeasy.com/# reputable indian online pharmacy

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

  37. Bradleyarrip说道:

    https://indianpharmacyeasy.com/# reputable indian online pharmacy

  38. Jefferyswava说道:

    ed pills: canadiandrugsgate.com – medications for

  39. LarryHer说道:

    natural ed pills: canadian drugs gate – online medications

  40. profi-teh-remont说道:

    Сервисный центр предлагает поменять заднюю крышку lexand lt-227 замена разъема питания lexand lt-227

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

  42. GlennSeade说道:

    pharmacies in mexico that ship to usa mexico pharmacy п»їbest mexican online pharmacies

  43. GlennSeade说道:

    best online pharmacies in mexico mexican pharmacy online buying prescription drugs in mexico online

  44. LarryHer说道:

    cure for ed: Canada pharmacy – homepage

  45. Jefferyswava说道:

    indianpharmacy com: indianpharmacyeasy.com – Online medicine home delivery

  46. Bradleyarrip说道:

    https://mexicanpharmgate.com/# best online pharmacies in mexico

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

  48. Bradleyarrip说道:

    http://indianpharmacyeasy.com/# top 10 online pharmacy in india

发表回复

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