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

/ 2,963评论 / 22813阅读 / 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. Timothyomide说道:

    http://cialisgenerico.life/# Farmacie on line spedizione gratuita

  2. Tomasdrads说道:

    viagra online consegna rapida: viagra senza ricetta – le migliori pillole per l’erezione

  3. Timothyomide说道:

    https://cialisgenerico.life/# farmacie online affidabili

  4. RandyBulty说道:

    Farmacia online miglior prezzo: kamagra gel prezzo – Farmacia online miglior prezzo

  5. RandyBulty说道:

    top farmacia online: Cialis generico prezzo – farmaci senza ricetta elenco

  6. RandyBulty说道:

    comprare farmaci online con ricetta: Avanafil 50 mg – acquisto farmaci con ricetta

  7. Tomasdrads说道:

    acquisto farmaci con ricetta: super kamagra – migliori farmacie online 2024

  8. Tomasdrads说道:

    Farmacia online piГ№ conveniente: Tadalafil generico migliore – farmacie online autorizzate elenco

  9. Timothyomide说道:

    https://cialisgenerico.life/# Farmacia online miglior prezzo

  10. Tomasdrads说道:

    farmacia online piГ№ conveniente: kamagra – migliori farmacie online 2024

  11. Timothyomide说道:

    https://farmait.store/# acquistare farmaci senza ricetta

  12. RandyBulty说道:

    farmacia online piГ№ conveniente: Farmacia online piu conveniente – acquistare farmaci senza ricetta

  13. RandyBulty说道:

    viagra originale in 24 ore contrassegno: farmacia senza ricetta recensioni – miglior sito dove acquistare viagra

  14. RandyBulty说道:

    comprare farmaci online con ricetta: Avanafil 50 mg – acquistare farmaci senza ricetta

  15. Tomasdrads说道:

    top farmacia online: Cialis generico prezzo – acquisto farmaci con ricetta

  16. Tomasdrads说道:

    farmacie online affidabili: Avanafil compresse – farmacie online autorizzate elenco

  17. Tomasdrads说道:

    farmacie online affidabili: farmacia online migliore – Farmacia online piГ№ conveniente

  18. Timothyomide说道:

    http://farmait.store/# Farmacia online piГ№ conveniente

  19. Tomasdrads说道:

    viagra 100 mg prezzo in farmacia: viagra online – gel per erezione in farmacia

  20. RandyBulty说道:

    Farmacia online miglior prezzo: Cialis generico recensioni – farmacia online

  21. Timothyomide说道:

    https://farmait.store/# farmacie online autorizzate elenco

  22. RandyBulty说道:

    miglior sito per comprare viagra online: viagra farmacia – viagra online in 2 giorni

  23. RandyBulty说道:

    farmacia online: kamagra gel – Farmacia online piГ№ conveniente

  24. Tomasdrads说道:

    kamagra senza ricetta in farmacia: acquisto viagra – viagra naturale in farmacia senza ricetta

  25. Tomasdrads说道:

    Farmacia online piГ№ conveniente: Farmacia online migliore – migliori farmacie online 2024

  26. Timothyomide说道:

    https://farmait.store/# acquistare farmaci senza ricetta

  27. Tomasdrads说道:

    farmacia online senza ricetta: Avanafil 50 mg – Farmacie on line spedizione gratuita

  28. RandyBulty说道:

    cerco viagra a buon prezzo: viagra prezzo – viagra naturale

  29. Tomasdrads说道:

    viagra 100 mg prezzo in farmacia: farmacia senza ricetta recensioni – cerco viagra a buon prezzo

  30. Timothyomide说道:

    http://cialisgenerico.life/# Farmacie online sicure

  31. RandyBulty说道:

    farmacie online sicure: kamagra gold – farmacie online affidabili

  32. RandyBulty说道:

    acquisto farmaci con ricetta: Farmacie che vendono Cialis senza ricetta – farmacie online affidabili

  33. Matthewnouts说道:

    tamoxifen effectiveness: tamoxifenworld – nolvadex pills

  34. Matthewnouts说道:

    prednisone 40 mg tablet: how much is prednisone 10mg – 200 mg prednisone daily

  35. LewisToite说道:

    https://zithromaxbestprice.pro/# generic zithromax 500mg

  36. Matthewnouts说道:

    buy misoprostol over the counter: cytotec online – purchase cytotec

  37. Matthewnouts说道:

    tamoxifen endometriosis: tamoxifen benefits – tamoxifen and grapefruit

  38. Matthewnouts说道:

    buy prednisone with paypal canada: prednisone 5084 – can i buy prednisone over the counter in usa

  39. Matthewnouts说道:

    where can i buy zithromax uk: where can i buy zithromax in canada – where can i get zithromax

  40. Matthewnouts说道:

    tamoxifen and ovarian cancer: tamoxifen dosage – how to get nolvadex

  41. Matthewnouts说道:

    cost of cheap propecia price: order propecia pills – generic propecia prices

  42. LewisToite说道:

    http://zithromaxbestprice.pro/# zithromax capsules 250mg

  43. LewisToite说道:

    https://nolvadexbestprice.pro/# tamoxifen alternatives premenopausal

  44. Matthewnouts说道:

    buy cytotec pills online cheap: Abortion pills online – buy cytotec

  45. Matthewnouts说道:

    cytotec abortion pill: cytotec abortion pill – purchase cytotec

  46. Matthewnouts说道:

    prednisone steroids: prednisone online for sale – over the counter prednisone pills

  47. Matthewnouts说道:

    get generic propecia without rx: buying cheap propecia no prescription – cost propecia without prescription

  48. LewisToite说道:

    https://zithromaxbestprice.pro/# zithromax 500 tablet

  49. rubmd说道:

    Wow wonderful blog layout How long have you been blogging for you make blogging look easy The overall look of your site is great as well as the content

  50. Matthewnouts说道:

    buy prednisone 10 mg: prednisone 20 mg tablets – prednisone tablets 2.5 mg

发表回复

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