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

/ 3,143评论 / 23618阅读 / 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. Nicolasburne说道:

    https://semaglutide.win/# Rybelsus 14 mg price

  2. Samueltib说道:

    https://gabapentin.auction/# neurontin price uk

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

  4. Samueltib说道:

    https://amoxil.llc/# amoxicillin 500

  5. магазин сейфов предлагает купить сейф 3 класса сейф 3 класс взломостойкости

  6. Stevenmut说道:

    https://stromectol.agency/# minocycline warnings
    zithromax z-pak price without insurance

  7. Matthewobjet说道:

    neurontin oral: buy gabapentin – neurontin buy online

  8. MelvinKer说道:

    Gay Boys Porn https://gay0day.com HD is the best gay porn tube to watch high definition videos of horny gay boys jerking, sucking their mates and fucking on webcam

  9. Michaelexiny说道:

    amoxicillin 500mg capsules uk amoxicillin 500mg cost price of amoxicillin without insurance

  10. Matthewobjet说道:

    rybelsus generic: buy rybelsus – Semaglutide pharmacy price

  11. магазин сейфов предлагает сейф взломостойкий 3 класс купить сейф 3 класс в москве

  12. Stevenmut说道:

    https://amoxil.llc/# buy amoxicillin online uk
    purchase zithromax z-pak

  13. Nicolasburne说道:

    https://amoxil.llc/# amoxicillin buy canada

  14. Nicolasburne说道:

    https://amoxil.llc/# amoxicillin brand name

  15. Stevenmut说道:

    https://semaglutide.win/# Rybelsus 14 mg price
    zithromax tablets for sale

  16. Matthewobjet说道:

    Rybelsus 14 mg price: Semaglutide pharmacy price – order Rybelsus for weight loss

  17. Michaelexiny说道:

    stromectol in canada generic name for ivermectin stromectol ivermectin 3 mg

  18. Stevenmut说道:

    http://amoxil.llc/# cost of amoxicillin 30 capsules
    zithromax coupon

  19. Matthewobjet说道:

    neurontin online: neurontin 1200 mg – neurontin for sale online

  20. Michaelexiny说道:

    buy stromectol pills stromectol best price ivermectin 0.1 uk

  21. Stevenmut说道:

    http://stromectol.agency/# generic stromectol
    buy zithromax online with mastercard

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

  23. Michaelexiny说道:

    amoxicillin where to get cheapest amoxil amoxicillin tablet 500mg

  24. Matthewobjet说道:

    ivermectin 50ml: buy minocycline 100mg tablets – stromectol 15 mg

  25. Stevenmut说道:

    https://gabapentin.auction/# brand name neurontin price
    zithromax order online uk

  26. Matthewobjet说道:

    ivermectin buy canada: minocycline 100mg pills – buy ivermectin

  27. Nicolasburne说道:

    http://stromectol.agency/# buy stromectol

  28. Michaelexiny说道:

    neurontin uk order gabapentin purchase neurontin canada

  29. Nicolasburne说道:

    https://amoxil.llc/# amoxicillin over counter

  30. Samueltib说道:

    https://stromectol.agency/# stromectol where to buy

  31. Stevenmut说道:

    https://semaglutide.win/# Semaglutide pharmacy price
    where can i buy zithromax in canada

  32. Nicolasburne说道:

    http://stromectol.agency/# ivermectin 2%

  33. Michaelexiny说道:

    neurontin brand name in india buy gabapentin buy gabapentin online

  34. Nicolasburne说道:

    https://zithromax.company/# where can i buy zithromax capsules

  35. Matthewobjet说道:

    buy azithromycin zithromax: zithromax for sale – buy zithromax online australia

  36. Matthewobjet说道:

    ivermectin 5ml: order stromectol – where to buy ivermectin cream

  37. Stevenmut说道:

    http://stromectol.agency/# ivermectin pills
    buy zithromax online

  38. Nicolasburne说道:

    https://gabapentin.auction/# neurontin 30 mg

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

  40. Nicolasburne说道:

    https://gabapentin.auction/# neurontin canada online

  41. Stevenmut说道:

    https://gabapentin.auction/# medicine neurontin 300 mg
    buy zithromax 1000 mg online

  42. Michaelexiny说道:

    300 mg neurontin gabapentin for sale ordering neurontin online

  43. Matthewobjet说道:

    amoxicillin 50 mg tablets: cheapest amoxil – medicine amoxicillin 500

  44. Stevenmut说道:

    https://zithromax.company/# zithromax for sale online
    zithromax 500 mg lowest price drugstore online

  45. Matthewobjet说道:

    Buy semaglutide pills: Semaglutide pharmacy price – cheap Rybelsus 14 mg

  46. Nicolasburne说道:

    https://zithromax.company/# zithromax for sale usa

发表回复

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