初尝AndroidStudio2.2+cmake+ndk开发(3)——FFmpeg的HelloWorld

本例子简单的输出FFmpeg其配置信息等

怎么编译FFmpeg的so库,可以参考我之前的编译过程http://www.jianshu.com/p/228f38e9aa9c

项目的目录:

初尝AndroidStudio2.2+cmake+ndk开发(3)——FFmpeg的HelloWorld_第1张图片

把之前编译好ffmpemg的头文件放到include里,so文件按平台文件夹放好。这里只编译了armeabi平台的。jniLibs文件夹没有的话自己建一个,也可以在build.gradle里指定一个目录。这里jniLibs的路径是AS的默认配置,无需修改build.gradle

Activity代码:

public class FFmpegHelloActivity extends AppCompatActivity {

    static {
        System.loadLibrary("ffmpeg-hello");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ffmpeg_hello);
        TextView tv = (TextView) findViewById(R.id.tv_info);
        tv.setText(avcodeConfigFromJni());
    }

    public native String avcodeConfigFromJni();
}

cpp代码:
头文件ffmpeg.h

#include 

extern "C"
JNIEXPORT jstring JNICALL
Java_com_clam314_hellojni_FFmpegHelloActivity_avcodeConfigFromJni(JNIEnv *env, jobject instance);

源代码ffmpeg-hello.cpp:

#include 
#include "ffmpeg-hello.h"
extern "C"
{
#include 
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_clam314_hellojni_FFmpegHelloActivity_avcodeConfigFromJni(JNIEnv *env, jobject instance) {
    char info[10000] = { 0 };
    sprintf(info, "%s\n", avcodec_configuration());
    return env->NewStringUTF(info);
}

CmakeLists.txt

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

# 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 it for you.
# Gradle automatically packages shared libraries with your APK.
#设置一个路径。
set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)

add_library(
            avcodec-57
            SHARED
            IMPORTED)
set_target_properties( avcodec-57
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/${ANDROID_ABI}/libavcodec-57.so )

add_library(
            avfilter-6
            SHARED
            IMPORTED)
set_target_properties( avfilter-6
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/${ANDROID_ABI}/libavfilter-6.so )

add_library(
            avformat-57
            SHARED
            IMPORTED)
set_target_properties( avformat-57
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/${ANDROID_ABI}/libavformat-57.so )

add_library(
            avutil-55
            SHARED
            IMPORTED)
set_target_properties( avutil-55
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/${ANDROID_ABI}/libavutil-55.so )

add_library(
            swresample-2
            SHARED
            IMPORTED)
set_target_properties( swresample-2
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/${ANDROID_ABI}/libswresample-2.so )

add_library(
            swscale-4
            SHARED
            IMPORTED)
set_target_properties( swscale-4
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/${ANDROID_ABI}/libswscale-4.so )
add_library( avdevice-57
             SHARED
             IMPORTED)
set_target_properties( avdevice-57
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/${ANDROID_ABI}/libavdevice-57.so )

#支持-std=gnu++11
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

#判断编译器类型,如果是gcc编译器,则在编译选项中加入c++11支持
if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
    message(STATUS "optional:-std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)

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 )

add_library( native-lib
             SHARED
             src/main/cpp/native-lib.cpp )


add_library( ffmpeg-hello
             SHARED
             src/main/cpp/ffmpeg-hello.cpp )

include_directories(src/main/jniLibs/include/)

target_link_libraries( native-lib avcodec-57 avfilter-6 avformat-57 avutil-55 swresample-2 swscale-4 avdevice-57
                       ${log-lib} )

target_link_libraries( ffmpeg-hello avcodec-57 avfilter-6 avformat-57 avutil-55 swresample-2 swscale-4 avdevice-57
                       ${log-lib} )

CMAKE_SOURCE_DIR 是CmakeLists.txt所在目录,Cmake的默认值

将 distribution_DIR指定为jniLibs的路径,后面用于统一指定so库的路径
set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)
#添加一个so库,每一个都要添加,无论是自定义还是预编译的
add_library( avdevice-57#so文件的名字
             SHARED #类型是共享库
             IMPORTED)#预编译的so指定为IMPORTED,自定义的eg:ffmpeg-hello的就填路径

#每加入一个预定义的so库都需要执行这命令
set_target_properties( avdevice-57#so文件的名字
                       PROPERTIES IMPORTED_LOCATION
                      #so库所在路径,ANDROID_ABI是平台的名字,Cmake的默认值
                       ${distribution_DIR}/${ANDROID_ABI}/libavdevice-57.so )
指定预编译库的头文件路径,另外自定义的头文件放cpp文件夹里就行
include_directories(src/main/jniLibs/include/)
链接各个so库,每一个自定义的so,都需要和其用到so链接一遍。eg:这里的ffmpe-hello就用到了ffmpeg的so库,但没有用到native-lib,native-lib就无须在参数里面。若没有调用此命令,调用其他so库的方法的地方会报 error: undefined reference to 
target_link_libraries( ffmpeg-hello avcodec-57 avfilter-6 avformat-57 avutil-55 swresample-2 swscale-4 avdevice-57
                       ${log-lib} )

结果:

初尝AndroidStudio2.2+cmake+ndk开发(3)——FFmpeg的HelloWorld_第2张图片

你可能感兴趣的:(初尝AndroidStudio2.2+cmake+ndk开发(3)——FFmpeg的HelloWorld)