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

/ 2,982评论 / 22896阅读 / 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. Профессиональный сервисный центр по ремонту ноутбуков и компьютеров.дронов.
    Мы предлагаем:ремонт ноутбуков
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  2. KevinOvems说道:

    https://slotsiteleri.bid/# slot casino siteleri

  3. DarrelTrini说道:

    en guvenilir slot siteleri: slot siteleri guvenilir – guvenilir slot siteleri 2024

  4. DarrelTrini说道:

    canl? slot siteleri: canl? slot siteleri – slot oyunlar? siteleri

  5. Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
    Мы предлагаем: ремонт планшетов apple в москве
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  6. DarrelTrini说道:

    deneme bonusu veren siteler: slot oyun siteleri – guvenilir slot siteleri

  7. KevinOvems说道:

    https://sweetbonanza.network/# pragmatic play sweet bonanza

  8. twinklecrest说道:

    I loved as much as youll receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get bought an nervousness over that you wish be delivering the following unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike

  9. Thankyou for helping out, great information.

  10. DarrelTrini说道:

    slot casino siteleri: slot siteleri – yasal slot siteleri

  11. KevinOvems说道:

    http://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  12. DarrelTrini说道:

    canl? slot siteleri: oyun siteleri slot – guvenilir slot siteleri

  13. DarrelTrini说道:

    en iyi slot siteleri: bonus veren casino slot siteleri – slot kumar siteleri

  14. DarrelTrini说道:

    slot siteleri: slot kumar siteleri – deneme veren slot siteleri

  15. Профессиональный сервисный центр по ремонту холодильников и морозильных камер.
    Мы предлагаем: мастерска по ремонту холодильников
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  16. Dentavim review说道:

    Hello, Neat post. There’s a problem along with your site in web explorer, may check this?K IE still is the marketplace leader and a big component of other people will miss your magnificent writing due to this problem.

  17. DarrelTrini说道:

    slot kumar siteleri: slot siteleri bonus veren – guvenilir slot siteleri

  18. meagree说道:

    I have high FSH and low estradiol and am taking Tamoxifen to hopefully ovulate a quality egg before my ovaries totally give up do i need a doctor prescription to buy priligy This patient, for instance, had notable ascites and lab values demonstrating hemoconcentration

  19. DarrelTrini说道:

    yeni slot siteleri: yeni slot siteleri – bonus veren casino slot siteleri

  20. DarrelTrini说道:

    en yeni slot siteleri: slot oyun siteleri – deneme bonusu veren siteler

  21. DarrelTrini说道:

    deneme bonusu veren slot siteleri: oyun siteleri slot – bonus veren slot siteleri

  22. DarrelTrini说道:

    oyun siteleri slot: en iyi slot siteler – oyun siteleri slot

  23. KevinOvems说道:

    http://sweetbonanza.network/# sweet bonanza giris

  24. KevinOvems说道:

    http://slotsiteleri.bid/# slot oyun siteleri

  25. DarrelTrini说道:

    casino slot siteleri: slot oyun siteleri – slot siteleri 2024

  26. DarrelTrini说道:

    slot siteleri bonus veren: oyun siteleri slot – canl? slot siteleri

  27. DarrelTrini说道:

    en iyi slot siteler: guvenilir slot siteleri – slot kumar siteleri

  28. KevinOvems说道:

    https://slotsiteleri.bid/# yasal slot siteleri

  29. DarrelTrini说道:

    slot oyun siteleri: en cok kazandiran slot siteleri – slot casino siteleri

  30. mlbbox说道:

    I conceive this internet site has got some rattling excellent info for everyone : D.

  31. KevinOvems说道:

    https://slotsiteleri.bid/# 2024 en iyi slot siteleri

  32. KevinOvems说道:

    http://slotsiteleri.bid/# casino slot siteleri

  33. DarrelTrini说道:

    bonus veren slot siteleri: slot siteleri 2024 – yasal slot siteleri

  34. DarrelTrini说道:

    slot siteleri 2024: deneme bonusu veren siteler – deneme bonusu veren slot siteleri

  35. KevinOvems说道:

    http://slotsiteleri.bid/# deneme veren slot siteleri

  36. Профессиональный сервисный центр по ремонту ноутбуков и компьютеров.дронов.
    Мы предлагаем:ремонт ноутбуков в москве рядом
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  37. 먹튀검증说道:

    you are truly a just right webmaster The site loading speed is incredible It kind of feels that youre doing any distinctive trick In addition The contents are masterwork you have done a great activity in this matter

  38. Профессиональный сервисный центр по ремонту ноутбуков, imac и другой компьютерной техники.
    Мы предлагаем:ремонт аймак москва
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  39. BaddieHub说道:

    BaddieHub This is my first time pay a quick visit at here and i am really happy to read everthing at one place

  40. DarrelTrini说道:

    slot siteleri guvenilir: en guvenilir slot siteleri – guvenilir slot siteleri 2024

  41. DarrelTrini说道:

    deneme bonusu veren siteler: en iyi slot siteler – slot oyun siteleri

  42. DarrelTrini说道:

    guvenilir slot siteleri: slot bahis siteleri – slot siteleri

  43. DarrelTrini说道:

    yeni slot siteleri: en yeni slot siteleri – guvenilir slot siteleri

发表回复

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