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

/ 2,966评论 / 22828阅读 / 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. CharlesLem说道:

    http://indiapharmacy.shop/# reputable indian pharmacies
    top rated ed pills

  2. CharlesLem说道:

    http://edpillpharmacy.store/# erectile dysfunction medicine online
    cheapest erectile dysfunction pills

  3. Douglasnug说道:

    medicine in mexico pharmacies: Purple pharmacy online ordering – buying from online mexican pharmacy

  4. CharlesLem说道:

    http://indiapharmacy.shop/# Online medicine order
    buy ed medication online

  5. CharlesLem说道:

    http://indiapharmacy.shop/# indian pharmacy online
    cheapest online ed meds

  6. Douglasnug说道:

    buying from online mexican pharmacy: Medicines Mexico – mexico pharmacies prescription drugs

  7. Leonardskarm说道:

    https://mexicopharmacy.win/# purple pharmacy mexico price list

  8. Leonardskarm说道:

    https://edpillpharmacy.store/# buy erectile dysfunction treatment

  9. Douglasnug说道:

    ed medications online: Cheap ED pills online – cheap ed treatment

  10. Leonardskarm说道:

    https://edpillpharmacy.store/# online erectile dysfunction prescription

  11. Georgemuh说道:

    paypal cialis payment: Generic Tadalafil 20mg price – cialis viagra mail order uk discrete billing

  12. Georgemuh说道:

    free viagra: Cheap Viagra online – buy viagra professional

  13. Samuelrip说道:

    https://tadalafil.auction/# cialis cost in nz

  14. Samuelrip说道:

    https://sildenafil.llc/# buy viagra order

  15. Georgemuh说道:

    how does viagra work: Viagra online price – viagra canada

  16. Georgemuh说道:

    cheap viagra: buy sildenafil online usa – buy generic viagra online

  17. Georgemuh说道:

    generic viagra overnight: Cheap Viagra online – cost of viagra

  18. Georgemuh说道:

    viagra without a doctor prescription usa: Buy Viagra online in USA – buy viagra online

  19. Samuelrip说道:

    http://sildenafil.llc/# viagra without doctor prescription

  20. Samuelrip说道:

    http://sildenafil.llc/# natural viagra

  21. Samuelrip说道:

    http://sildenafil.llc/# cialis vs viagra

  22. Georgemuh说道:

    viagra coupon: buy sildenafil online usa – real viagra without a doctor prescription

  23. Samuelrip说道:

    http://tadalafil.auction/# cialis 20mg sell

  24. Georgemuh说道:

    ed pills that work better than viagra: Buy Viagra online in USA – viagra cost

  25. Georgemuh说道:

    viagra generic: Buy Viagra online in USA – viagra price

  26. web page说道:

    Very interesting subject, appreciate it for posting.Blog monetyze

  27. Georgemuh说道:

    cheap cialis super active: Buy Tadalafil 20mg – cialis with no prescription

  28. Samuelrip说道:

    http://sildenafil.llc/# 100mg viagra without a doctor prescription

  29. Georgemuh说道:

    viagra pills: buy sildenafil online usa – viagra

  30. Georgemuh说道:

    cialis 20 mg from united kingdom: Buy Cialis online – cialis payment with paypal

  31. Samuelrip说道:

    https://tadalafil.auction/# canadian meds cialis

  32. LesterhIz说道:

    farmacie online sicure: Cialis generico 5 mg prezzo – п»їFarmacia online migliore

  33. Timothyomide说道:

    http://kamagrait.pro/# farmacie online sicure

  34. Tomasdrads说道:

    п»їFarmacia online migliore: avanafil senza ricetta – farmacia online

  35. Tomasdrads说道:

    farmacie online sicure: Farmacia online piu conveniente – farmacia online

  36. Timothyomide说道:

    http://cialisgenerico.life/# farmaci senza ricetta elenco

  37. Tomasdrads说道:

    acquisto farmaci con ricetta: Cialis generico 20 mg 8 compresse prezzo – farmacia online senza ricetta

  38. RandyBulty说道:

    viagra ordine telefonico: viagra prezzo – cialis farmacia senza ricetta

  39. Tomasdrads说道:

    acquistare farmaci senza ricetta: Cialis generico farmacia – farmaci senza ricetta elenco

  40. RandyBulty说道:

    farmacie online autorizzate elenco: sildenafil oral jelly 100mg kamagra – farmacia online piГ№ conveniente

  41. RandyBulty说道:

    farmacie online sicure: avanafil senza ricetta – top farmacia online

  42. Timothyomide说道:

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

  43. RandyBulty说道:

    migliori farmacie online 2024: Farmacie online sicure – Farmacie online sicure

  44. RandyBulty说道:

    acquisto farmaci con ricetta: Avanafil prezzo – acquisto farmaci con ricetta

  45. RandyBulty说道:

    farmacie online autorizzate elenco: super kamagra – Farmacia online miglior prezzo

  46. Tomasdrads说道:

    farmacia online piГ№ conveniente: Avanafil prezzo – comprare farmaci online con ricetta

  47. Tomasdrads说道:

    viagra online spedizione gratuita: acquisto viagra – viagra online spedizione gratuita

  48. Tomasdrads说道:

    farmacia online piГ№ conveniente: Cialis generico controindicazioni – Farmacie online sicure

发表回复

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