Create an Android C++ support project
Directly create C++ support projects, you can write less configuration, novice can directly create. To make it easier, create a C++ support project and you’ll see a CPP folder under main, as well as the cmake configuration file
The introduction of FFmpeg
Before FFmpeg can be introduced, it needs to be downloaded and compiled. Download and compile are not discussed here. If necessary, you can download the compiled library directly from Github
I’m using a static library compiled from version 4.4 that I downloaded from Git. As long as the 4.0 and later versions are not significantly different, these are not discussed in this article. This article will only consider how to configure it on Android. And run the display version number
Files to use
Ffmpeg header files, in the include folder, static library files in the corresponding CPU architecture folder, arm64-V8A as an example, due to non-describable reasons. It is recommended that arm64-V8A should be supported
The location of the file
Here I will create an FFmpeg folder in CPP for header files and static libraries. This is enough to configure cmake
Cmake_minimum_required (VERSION 3.10.2) Project ("music") file(GLOB src_files *.cpp *.c *.h) add_library(music SHARED ${src_files}) include_directories(${CMAKE_SOURCE_DIR}/ffmpeg/include) Set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -l ${CMAKE_SOURCE_DIR}/ffmpeg/${CMAKE_ANDROID_ARCH_ABI}") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI}") target_link_libraries( # Specifies the target library. music log -Wl,--start-group avformat avcodec avfilter avutil swresample swscale -Wl,--end-group z android OpenSLES )Copy the code
Configuring Gradle is very simple
Just add a few lines of code and that’s the main thing
abiFilters "arm64-v8a"
Copy the code
This line of code is basically going to be OK down here
android { compileSdk 31 defaultConfig { applicationId "com.androideasy.music" minSdk 21 targetSdk 31 versionCode 1 VersionName "1.0" testInstrumentationRunner "androidx. Test. Runner. AndroidJUnitRunner" externalNativeBuild {cmake {/ / CppFlags '' abiFilters "arm64-v8A"}} NDK {// Specifies that the CPU architecture is armeabi-v7A Armeabi-v7a into apK 】 abiFilters("arm64-v8a")}}}Copy the code
Based on the above file configuration basic can run, if you have any questions, or missed content, please leave a message will be added as soon as possible, thank you very much.
Write a test method
In c++ support projects, you’re going to call a hello world method by default and we’re going to delete that, and we’re going to create a new local method that’s going to call display. The Kotlin /Java layer code is very simple
//kotlin
public external fun getFFmpegVersions(): String
//Java
public native String getFFmpegVersions();
Copy the code
This calls CPP code through JNI
Java_com_androideasy_music_MusicPlayer_getFFmpegVersions
This method name will be created according to your needs. Run this to see the version version
#include <jni.h>
#include <string>
#include <string>
extern "C"{
#include <libavformat/avformat.h>
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_androideasy_music_MusicPlayer_getFFmpegVersions(JNIEnv *env, jobject thiz) {
std::string version = av_version_info();
return env->NewStringUTF(version.c_str());
}
Copy the code