Android 调用ffmpeg或者打包成jar

首先创建一个c++的 Android native项目, 将编译好的so放入指定目录

修改gradle文件
android {
    ...
    sourceSets {
        main {
            jniLibs.srcDirs = ['jniLibs']
        }
    }
}
选择cpu架构
defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags '-frtti -fexceptions'
                abiFilters "armeabi-v7a","arm64-v8a"
            }
        }
        ndk {
            abiFilters "armeabi-v7a","arm64-v8a"
        }
    }
复制头文件

配置CmakeLists
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.18.1)

# Declares and names the project.

project("libffmpeg")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.


include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

set(ffmpeg_lib_dir ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI})
# 这一句用来导出编译生成的so库   不然打包jar,调用会出错not find so  
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI})


add_library( # Sets the name of the library.
        libffmpeg

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        libffmpeg.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.


add_library( avutil
        SHARED
        IMPORTED )
set_target_properties( avutil
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libavutil.so )

add_library( swresample
        SHARED
        IMPORTED )
set_target_properties( swresample
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libswresample.so )

add_library( avcodec
        SHARED
        IMPORTED )
set_target_properties( avcodec
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libavcodec.so )

add_library( avfilter
        SHARED
        IMPORTED)
set_target_properties( avfilter
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libavfilter.so )

add_library( swscale
        SHARED
        IMPORTED)
set_target_properties( swscale
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libswscale.so )

add_library( avformat
        SHARED
        IMPORTED)
set_target_properties( avformat
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libavformat.so )


add_library( postproc
        SHARED
        IMPORTED)
set_target_properties( postproc
        PROPERTIES IMPORTED_LOCATION
        ${ffmpeg_lib_dir}/libpostproc.so )


find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        libffmpeg

        avutil
        swresample
        avcodec
        avfilter
        swscale
        avformat
        postproc

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
加载so
class NativeLib {

    /**
     * A native method that is implemented by the 'libffmpeg' native library,
     * which is packaged with this application.
     */

    companion object {
        // Used to load the 'libffmpeg' library on application startup.
        init {
            System.loadLibrary("libffmpeg")
        }
    }
}

在这就可以编写jni相关内容调用ffmpeg了

 

如果需要打包jar

需要输出so,不然打包jar,调用会出错not find so


# 这一句用来导出编译生成的so库
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI})

gradle添加以下

//打包jar,在gradle->build->other->makeJar
    task makeJar(type: Copy) {
        //删除存在的
        delete 'build/libs/libplayer.jar'
        //设置拷贝的文件
        from('build/intermediates/aar_main_jar/release/')
        //打进jar包后的文件目录
        into('build/libs/')
        //将classes.jar放入build/libs/目录下
        //include ,exclude参数来设置过滤
        include('classes.jar')
        //重命名
        rename ('classes.jar', 'libplayer.jar')
    }
    makeJar.dependsOn(build)

将jar包和so复制到需要调用的项目里并导入引用

dependencies {
    implementation fileTree(include:['*.jar'],dir:'libs')
    implementation files('libs/libplayer.jar')
}

 

posted @ 2023-05-22 10:12  西瓜皮不甜  阅读(125)  评论(0编辑  收藏  举报