引入步骤:
1.创建android支持c++的
2.导入编译之后的文件
image.png
2.导入源码
image.png
3.cmakefile
//使用的版本
cmake_minimum_required(VERSION 3.10.2)
//项目名称
project("ffmpeddemo")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(jnilibs ${CMAKE_SOURCE_DIR}/../libs)
set(libname native-lib)
include_directories(
include
${CMAKE_SOURCE_DIR}/util
${CMAKE_SOURCE_DIR}/player
${CMAKE_SOURCE_DIR}/player/render
)
link_directories(
${jnilibs}/${ANDROID_ABI})
file(GLOB src-files
${CMAKE_SOURCE_DIR}/*.cpp
${CMAKE_SOURCE_DIR}/util/*.cpp
${CMAKE_SOURCE_DIR}/player/*.cpp
${CMAKE_SOURCE_DIR}/player/decoder/*.cpp
${CMAKE_SOURCE_DIR}/player/render/video/*.cpp
${CMAKE_SOURCE_DIR}/player/render/audio/*.cpp
${CMAKE_SOURCE_DIR}/recorder/SingleVideoRecorder.cpp
${CMAKE_SOURCE_DIR}/recorder/SingleAudioRecorder.cpp
${CMAKE_SOURCE_DIR}/recorder/MediaRecorderContext.cpp
${CMAKE_SOURCE_DIR}/recorder/GLCameraRender.cpp
${CMAKE_SOURCE_DIR}/recorder/MediaRecorder.cpp
)
//是添加自己导入或者编写的C++源文件的作用native-lib:这个是编译成so库的名称,生成的so会在前面加lib,最后生成的so:libnative-lib.so
add_library( # Sets the name of the library.
${libname}
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${src-files}
)
set(third-party-libs
avformat
avcodec
avfilter
swresample
swscale
avutil
fdk-aac
x264
)
set(native-libs
android
EGL
GLESv3
OpenSLES
log
m
z
)
//支持的库
target_link_libraries( # Specifies the target library.
${libname}
# Links the target library to the log library
# included in the NDK.
${log-lib}
${third-party-libs}
${native-libs}
)
4.编写cpp
extern "C" JNIEXPORT jstring JNICALL
Java_com_kangwang_ffmpeddemo_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(av_version_info());
}
5,java代码
@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());
}
public native String stringFromJNI();
6.安装手机会打印版本。
网友评论