Android Studio 导入ffmpeg库 -- CMakeLists方式

前面已经编译成功了,在编译目录将 include 和 lib 文件拷贝出来
上一篇编译ffmpeg

 android/arm/*

文件的放置和创建

将以下文件以及include文件拷贝到对应文件夹:

	libavcodec.so-58.so
	libavdevice.so-58.so
	libavfilter.so-7.so
	libavformat.so-58.so
	libavutil.so-56.so
	libswresample.so-3.so
	libswscale.so-5.so

在这里插入图片描述
在app下新建一个 CMakeLists.txt

	cmake_minimum_required(VERSION 3.4.1)
	set(lib_src_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
	include_directories(
	        ${CMAKE_SOURCE_DIR}/src/main/cpp/include
	)
	add_library(avcodec-58_lib SHARED IMPORTED)
	set_target_properties(avcodec-58_lib PROPERTIES IMPORTED_LOCATION
	                             ${lib_src_DIR}/libavcodec.so-58.so)
	add_library(avformat-58_lib SHARED IMPORTED)
	set_target_properties(avformat-58_lib PROPERTIES IMPORTED_LOCATION
	                        ${lib_src_DIR}/libavformat.so-58.so)
	add_library(avutil-56_lib SHARED IMPORTED)
	set_target_properties(avutil-56_lib PROPERTIES IMPORTED_LOCATION
	                        ${lib_src_DIR}/libavutil.so-56.so)
	add_library(swresample-3_lib SHARED IMPORTED)
	set_target_properties(swresample-3_lib PROPERTIES IMPORTED_LOCATION
	                        ${lib_src_DIR}/libswresample.so-3.so)
	add_library(swscale-5_lib SHARED IMPORTED)
	set_target_properties(swscale-5_lib PROPERTIES IMPORTED_LOCATION
	                        ${lib_src_DIR}/libswscale.so-5.so)
	
	add_library(native-lib SHARED
	            ${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp)
	
	target_link_libraries(native-lib
	log
	android
	avcodec-58_lib
	avformat-58_lib
	avutil-56_lib
	swresample-3_lib
	swscale-5_lib)

新建 native-lib.cpp 文件

	#include <jni.h>
	extern "C"{
	#include "libavcodec/avcodec.h"
	}
	
	extern "C"
	jstring
	Java_com_test_myplayer_MyPlayer_stringFromJNI(JNIEnv *env, jobject /* this */) {
	    char info[10000] = { 0 };
	    sprintf(info, "%s\n", avcodec_configuration());
	    return env->NewStringUTF(info);
	}

新建 MyPlayer.java

	package com.test.myplayer;
	public class MyPlayer {
	    static {
	        System.loadLibrary("native-lib");
	    }
	    public native String stringFromJNI();
	}

编译配置

在 app/build.gradle 下增加配置

android {
    ...
    defaultConfig {
    ...
		ndk {
            abiFilters 'armeabi-v7a'
		}
		externalNativeBuild {
	    cmake {
	        cppFlags "-std=c++11"
	    }
	...
	externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

猜你喜欢

转载自blog.csdn.net/LHshooter/article/details/107562733
今日推荐