新建项目VoicePlayer,项目包括主app和一个库voicelib,所有关于操作音频、jni的编写都在voicelib库中
注意在新建module的时候使用的是第二个,不要搞错
看一下我们的工程目录:
CMakeList.txt
cmake_minimum_required(VERSION 3.4.1)
#导入include路径
include_directories(src/main/cpp/include)
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 )
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 )
#添加动态库
add_library( avcodec-57 SHARED IMPORTED)
#设置动态库路径
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavcodec-57.so)
add_library( avdevice-57 SHARED IMPORTED)
set_target_properties( avdevice-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavdevice-57.so)
add_library( avfilter-6 SHARED IMPORTED)
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavfilter-6.so)
add_library( avformat-57 SHARED IMPORTED)
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavformat-57.so)
add_library( avutil-55 SHARED IMPORTED)
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavutil-55.so)
add_library( postproc-54 SHARED IMPORTED)
set_target_properties( postproc-54
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libpostproc-54.so)
add_library( swresample-2 SHARED IMPORTED)
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libswresample-2.so)
add_library( swscale-4 SHARED IMPORTED)
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libswscale-4.so)
target_link_libraries( # Specifies the target library.
native-lib
avcodec-57
avdevice-57
avfilter-6
avformat-57
avutil-55
postproc-54
swresample-2
swscale-4
# Links the target library to the log library
# included in the NDK.
${log-lib} )
Player.java
package com.example.voicelib;
/**
* 作者 huozhenpeng
* 日期 2018/10/18
* 邮箱 huohacker@sina.com
*/
public class Player {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
System.loadLibrary("avutil-55");
System.loadLibrary("swresample-2");
System.loadLibrary("avcodec-57");
System.loadLibrary("avformat-57");
System.loadLibrary("swscale-4");
System.loadLibrary("postproc-54");
System.loadLibrary("avfilter-6");
System.loadLibrary("avdevice-57");
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
/**
* 测试集成ffmpeg,打印ffmpeg支持的所有解码器
*/
public native void printCodec();
}
native-lib.cpp
#include <jni.h>
#include <string>
#include <android/log.h>
extern "C"
{
#include <libavformat/avformat.h>
}
#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"VoicePlayer",FORMAT,##__VA_ARGS__);
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_voicelib_Player_stringFromJNI(JNIEnv *env, jobject instance) {
// TODO
std::string hello = "Hello from C++";
av_register_all();
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_voicelib_Player_printCodec(JNIEnv *env, jobject instance) {
av_register_all();
AVCodec *codec=av_codec_next(NULL);
while (codec!=NULL)
{
switch (codec->type)
{
case AVMEDIA_TYPE_VIDEO:
LOGI("[Video]:%s",codec->name);
break;
case AVMEDIA_TYPE_AUDIO:
LOGI("[Audio]:%s", codec->name);
break;
default:
LOGI("[Other]:%s", codec->name);
break;
}
codec=codec->next;
}
}
主项目MainActivity调用
package com.example.huozhenpeng.voiceplayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.voicelib.Player;
public class MainActivity extends AppCompatActivity {
@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);
Player player=new Player();
tv.setText(player.stringFromJNI());
player.printCodec();
}
}
输出: