ffmpeg.so在Android中的使用
程序员文章站
2022-07-13 13:32:12
...
- 如果你还没有FFmpeg.so文件,前请往获取:FFmpeg编译Android端so文件纪要(附资源下载)
前言: 在这个过程中请尽量克制你的小心思,按照步骤走通后再展现你的骚操作。
1、AndroidStudio创建C++工程
2、拷贝include文件夹(跟ffmpeg一起生成的)以及ffmpeg.so文件到工程中
3、打开CMakeLists文件,加入ffmpeg,如下:
- ${CMAKE_SOURCE_DIR} 标识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.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 them for you.
#Gradle automatically packages shared libraries with your APK.
#创建和命名一个库,将其设置为静态的
#或共享,并提供到其源代码的相对路径。
#您可以定义多个库,然后CMake为您构建它们。
#Gradle自动将共享库打包到你的APK中。
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp)
####### 这里是新加的 #######
add_library( ffmpeg
SHARED
IMPORTED)
set_target_properties( ffmpeg
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/arm64-v8a/libffmpeg.so)
#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.
#搜索指定的预构建库并将路径存储为
#变量。因为CMake在搜索路径中包含了系统库
#默认情况下,您只需要指定公共NDK库的名称
#要添加。CMake验证以前是否存在该库
#完成它的构建。
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.
#指定CMake应该链接到目标库的库。你
#可以链接多个库,比如您在本文中定义的库吗
#构建脚本、预构建的第三方库或系统库。
####### 这里是新加的 #######
include_directories(src/main/cpp/include/)
target_link_libraries( # Specifies the target library.
native-lib
ffmpeg
# Links the target library to the log library
# included in the NDK.
${log-lib})
4、修改完成后先build一下工程,保证到这一步没有错误,如果出错可以按以下方法排查
- 检查地址引用的地址是否正确
- 从最后开始一段一段注释掉代码,通过构建检查是否有错误
5、再确保以构建没有问题之后进入native-lib.cpp,修改如下:
- so库中的头文件必须使用 extern “C” 包起来引用,千万不要使用Alt+Enter导入,否则会报错,错误为:
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library …\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_zhlm_myapplication_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
/*********************************** 以下是新加的 ***********************************/
// so库中的头文件必须使用 extern "C" 包起来引用,否则会报错
extern "C" {
#include "include/libavcodec/avcodec.h"
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_包名_包名_MainActivity_stringTest(JNIEnv *env,
jobject /* this */) {
char info[10000] = {0};
// 这里随便找了个文件打印部分内容,用来验证可以正常使用FFmpeg了
sprintf(info, "%s\n", avcodec_configuration());
return env->NewStringUTF(info);
}
同时在MainActivity中使用:
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI() + "\n" + stringTest());
}
/**
* A native method that is implemented by the 'native-lib' native library, which is packaged with this application.
*/
public native String stringFromJNI();
public native String stringTest(); // 这个就是在native_lib.cpp 中新加的方法
}
6、最后别忘了在app.build中加入:
defaultConfig {
applicationId "com.xxx.xxx"
***
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
ndk {
abiFilters "arm64-v8a"
}
}
// 必须有,否则so文件可能不会被打包进APK
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
7、好了,跑起来看看效果吧!