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

/ 537评论 / 6838阅读 / 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. kmtfirm说道:

    Hello my loved one I want to say that this post is amazing great written and include almost all significant infos I would like to look extra posts like this

  2. latestsession说道:

    I do agree with all the ideas you have introduced on your post They are very convincing and will definitely work Still the posts are very short for newbies May just you please prolong them a little from subsequent time Thank you for the post

  3. Jamesjer说道:

    cipro: buy ciprofloxacin over the counter – ciprofloxacin 500mg buy online

  4. Jamesjer说道:

    where can i buy cheap clomid without insurance: can i order generic clomid without a prescription – buy generic clomid

  5. Jamesjer说道:

    paxlovid cost without insurance: Paxlovid buy online – buy paxlovid online

  6. Jamesjer说道:

    buy cipro online canada: buy cipro – cipro online no prescription in the usa

  7. Jamesjer说道:

    get cheap clomid without dr prescription: how can i get generic clomid without prescription – where buy cheap clomid no prescription

  8. Jamesjer说道:

    cost of doxycycline in india: doxyhexal – buy online doxycycline without prescription

  9. Jamesjer说道:

    how to get cheap clomid online: order cheap clomid without rx – where to buy clomid without prescription

  10. ThomasInace说道:

    https://ciprodelivery.pro/# ciprofloxacin mail online

  11. ThomasInace说道:

    https://amoxildelivery.pro/# amoxicillin 500mg capsule buy online

  12. ThomasInace说道:

    http://clomiddelivery.pro/# can you buy generic clomid pill

  13. I really enjoy reading on this internet site, it holds excellent content. “The great secret of power is never to will to do more than you can accomplish.” by Henrik Ibsen.

  14. Jamesjer说道:

    doxycycline 100mg tabs: doxycycline tablet price in india – doxycycline 100mg capsules price

  15. Jamesjer说道:

    can you get cheap clomid without insurance: can you buy generic clomid pills – cost cheap clomid now

  16. ThomasInace说道:

    https://ciprodelivery.pro/# buy cipro online canada

  17. Jamesjer说道:

    buy cipro without rx: purchase cipro – buy ciprofloxacin over the counter

  18. Jamesjer说道:

    where can i get doxycycline online: how to get doxycycline online – doxycycline australia cost

  19. ThomasInace说道:

    http://clomiddelivery.pro/# cost cheap clomid no prescription

  20. Jamesjer说道:

    buy doxycycline 200 mg: doxycycline 75 mg cost – cost of doxycycline tablets

  21. Jamesjer说道:

    paxlovid buy: paxlovid generic – Paxlovid over the counter

  22. ThomasInace说道:

    http://clomiddelivery.pro/# buy clomid without prescription

  23. Jamesjer说道:

    can you buy generic clomid tablets: where to buy clomid without a prescription – can you buy generic clomid tablets

  24. webnontonbola说道:

    Terima kasih banyak telah berbagi, sepertinya menarik web nonton bola

  25. Jamesjer说道:

    paxlovid for sale: п»їpaxlovid – Paxlovid over the counter

  26. ThomasInace说道:

    http://ciprodelivery.pro/# cipro 500mg best prices

  27. ThomasInace说道:

    https://amoxildelivery.pro/# amoxicillin 750 mg price

  28. ThomasInace说道:

    http://ciprodelivery.pro/# cipro online no prescription in the usa

  29. youtube to mp3说道:

    Your writing has a way of resonating with me on a deep level. It’s clear that you put a lot of thought and effort into each piece, and it certainly doesn’t go unnoticed.

  30. Jamesjer说道:

    paxlovid for sale: paxlovid pill – paxlovid pharmacy

  31. Jamesjer说道:

    paxlovid pill: paxlovid for sale – paxlovid pill

  32. Tonic Greens说道:

    I truly enjoy looking at on this internet site, it has got fantastic posts.

  33. ThomasInace说道:

    http://ciprodelivery.pro/# ciprofloxacin 500 mg tablet price

  34. ThomasInace说道:

    http://clomiddelivery.pro/# where to buy clomid without insurance

  35. Jamesjer说道:

    paxlovid buy: Paxlovid buy online – buy paxlovid online

发表回复

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