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

/ 3,411评论 / 24570阅读 / 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. LarryBow说道:

    az parayla cok kazandiran slot oyunlar? slot siteleri az parayla cok kazandiran slot oyunlar?

  2. Тут можно преобрести оружейный сейф на заказ москва сейфы для оружия пистолетные

  3. LewisJow说道:

    укладка кафельной плитки квадратный метр https://ukladka-keramogranita-spb.ru

  4. Тут можно преобрести где купить сейф для ружья сейф шкаф купить

  5. BrandonGak说道:

    indian pharmacies safe: Pharmacies in India that ship to USA – online pharmacy india

  6. JorgeSEk说道:

    best india pharmacy: Best online Indian pharmacy – reputable indian pharmacies

  7. Williambiz说道:

    ed treatments that really work http://mexicanpharm24.cheap/# medicine in mexico pharmacies

  8. DevonStolf说道:

    http://indianpharm24.pro/# Online medicine order
    ed remedies

  9. BrandonGak说道:

    ed cures: cheap drugs – treatment of ed

  10. JorgeSEk说道:

    reputable mexican pharmacies online: Legit online Mexican pharmacy – mexican drugstore online

  11. JorgeSEk说道:

    best india pharmacy: indian pharmacy purchase online – indian pharmacies safe

  12. Williambiz说道:

    what type of medicine is prescribed for allergies https://indianpharm24.pro/# buy prescription drugs from india

  13. DevonStolf说道:

    https://pharm24.pro/# supplements for ed
    prescription drugs

  14. Williambiz说道:

    mens erection pills https://pharm24.pro/# ed help

  15. BrandonGak说道:

    male ed drugs: cheap prescription drugs – over the counter ed treatment

  16. JorgeSEk说道:

    drugs and medications: cheaper medications – remedies for ed

  17. BrandonGak说道:

    buying from online mexican pharmacy: mexican drugs – mexican pharmaceuticals online

  18. JorgeSEk说道:

    reputable mexican pharmacies online: mexico pharmacy – medicine in mexico pharmacies

  19. Leroytrupt说道:

    indian pharmacy Best online Indian pharmacy indian pharmacy online

  20. Leroytrupt说道:

    buying prescription drugs in mexico mexican pharmacy mexican drugstore online

  21. Williambiz说道:

    ed pills online http://indianpharm24.pro/# mail order pharmacy india

  22. DevonStolf说道:

    http://indianpharm24.pro/# Online medicine order
    viagra without a doctor prescription

  23. Leroytrupt说道:

    canada ed drugs affordable medication prices of viagra at walmart

  24. JorgeSEk说道:

    indian pharmacy paypal: Indian pharmacy worldwide delivery – online pharmacy india

  25. Leroytrupt说道:

    mail order pharmacy india Indian pharmacy international shipping online shopping pharmacy india

  26. BrandonGak说道:

    top 10 online pharmacy in india: Best Indian pharmacy – best online pharmacy india

  27. Leroytrupt说道:

    mexican rx online mexico pharmacy medication from mexico pharmacy

  28. JorgeSEk说道:

    what are ed drugs: cheap meds – prescription drugs online

  29. Leroytrupt说道:

    pharmacies in mexico that ship to usa mexico pharmacy buying prescription drugs in mexico

  30. Williambiz说道:

    prescription drugs without doctor approval http://mexicanpharm24.cheap/# medicine in mexico pharmacies

  31. DevonStolf说道:

    http://mexicanpharm24.cheap/# mexican pharmaceuticals online
    ed pills that work quickly

  32. BrandonGak说道:

    mexico drug stores pharmacies: mexican pharmacy – mexican rx online

  33. Williambiz说道:

    online prescription for ed meds http://indianpharm24.pro/# Online medicine home delivery

  34. JorgeSEk说道:

    medications list: cheap meds – drug prices comparison

  35. Leroytrupt说道:

    medication from mexico pharmacy mexican pharmacy purple pharmacy mexico price list

  36. Leroytrupt说道:

    Online medicine order Best online Indian pharmacy best online pharmacy india

  37. JorgeSEk说道:

    pharmacy website india: India pharmacy international – Online medicine order

  38. Williambiz说道:

    viagra without a doctor prescription https://pharm24.pro/# best non prescription ed pills

  39. BrandonGak说道:

    mexico drug stores pharmacies: mexican pharm 24 – best online pharmacies in mexico

  40. Leroytrupt说道:

    mexico drug stores pharmacies mexican pharmacy mexico drug stores pharmacies

  41. DevonStolf说道:

    https://mexicanpharm24.cheap/# mexican pharmaceuticals online
    male enhancement pills

  42. Leroytrupt说道:

    indianpharmacy com Best online Indian pharmacy reputable indian online pharmacy

  43. profi-teh-remont说道:

    Сервисный центр предлагает замена заднего стекла sony xperia xa2 замена разъема зарядки sony xperia xa2

  44. JorgeSEk说道:

    google viagra dosage recommendations: cheap drugs – ed pharmacy

  45. BrandonGak说道:

    Online medicine home delivery: Pharmacies in India that ship to USA – online pharmacy india

  46. Leroytrupt说道:

    vacuum pumps for ed low cost prescription pet meds without vet prescription

  47. Leroytrupt说道:

    indian pharmacy paypal indian pharm 24 indian pharmacy online

  48. Williambiz说道:

    cheap pet meds without vet prescription http://indianpharm24.pro/# mail order pharmacy india

发表回复

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