准备工作:
1.编译好的ffmpeg动态库.so文件,以及ffmpeg的头文件;
2.android studio开发环境,包括NDK等可用环境;
步骤一:
新建一个android studio工程,命名netplayer,勾选“Include C++ support”;其他选择默认,直到完成;
步骤二:
将ffmpeg的动态库和include头文件,拷贝到netplayer工程的libs目录下:
android studio会自动更新工程列表,后续主要修改以下几个文件:
//C++接口的实现,内部封装ffmpeg功能
E:\MyAndroidWorkSpace\netplayer\app\src\main\cpp\native-lib.cpp
//java界面,调用C++接口
E:\MyAndroidWorkSpace\netplayer\app\src\main\java\player\cn\com\netplayer\MainActivity.java
//编译参数,指定库
E:\MyAndroidWorkSpace\netplayer\app\CMakeLists.txt
//jni相关
E:\MyAndroidWorkSpace\netplayer\app\build.gradle
步骤三(CMakeList.txt)
cmake_minimum_required(VERSION 3.4.1)
find_library( log-lib
log )
set(distribution_DIR ../../../../libs)
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
add_library( avcodec-57
SHARED
IMPORTED)
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/armeabi/libavcodec-57.so)
add_library( avfilter-6
SHARED
IMPORTED)
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/armeabi/libavfilter-6.so)
add_library( avformat-57
SHARED
IMPORTED)
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/armeabi/libavformat-57.so)
add_library( avutil-55
SHARED
IMPORTED)
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/armeabi/libavutil-55.so)
add_library( swresample-2
SHARED
IMPORTED)
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/armeabi/libswresample-2.so)
add_library( swscale-4
SHARED
IMPORTED)
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/armeabi/libswscale-4.so)
include_directories(libs/include)
target_link_libraries( native-lib
avcodec-57
avfilter-6
avformat-57
avutil-55
swresample-2
swscale-4
${log-lib} )
步骤四(build.gradle):
在系统生成的原来基础上,加入:
defaultConfig下加入:
sourceSets { main { jniLibs.srcDirs = ['libs'] } }
externalNativeBuild下加入:
ndk{
abiFilters "armeabi"
}
完整内容:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "player.cn.com.netplayer"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
externalNativeBuild {
cmake {
cppFlags ""
}
ndk{
abiFilters "armeabi"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
步骤5(native-lib.cpp)
#include
#include
#include
extern "C" {
//编码
#include "libavcodec/avcodec.h"
//封装格式处理
#include "libavformat/avformat.h"
//像素处理
#include "libswscale/swscale.h"
}
#define FFLOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"ffmpeg",FORMAT,##__VA_ARGS__);
#define FFLOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"ffmpeg",FORMAT,##__VA_ARGS__);
extern "C"
JNIEXPORT jstring JNICALL
Java_player_cn_com_netplayer_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jstring JNICALL
Java_player_cn_com_netplayer_MainActivity_InitFFMPEG(
JNIEnv* env,
jobject /* this */) {
//1.注册所有组件
av_register_all();
std::string hello = "av_register_all OK";
return env->NewStringUTF(hello.c_str());
}
package player.cn.com.netplayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
System.loadLibrary("avcodec-57");
System.loadLibrary("avfilter-6");
System.loadLibrary("avformat-57");
System.loadLibrary("avutil-55");
System.loadLibrary("swresample-2");
System.loadLibrary("swscale-4");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
tv.setText(InitFFMPEG());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
//初始化ffmpeg库
public native String InitFFMPEG();
}
Demo程序下载地址:https://github.com/zhuxian2009/netplayer