在Android里面集成ffmpeg使用的时候,遇到了很多问题,这里整理一下。
1.invalid suffix on literal; C++11 requires a space between literal and identifier
C++需要再字符和变量之间增加空格号
snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
需改为
snprintf(name, sizeof(name), "0x%" PRIx64, ch_layout);
2. error: expected ')'
C++无法解析class关键字导致
void show_help_children(const AVClass * class, int flags);
需要改为
void show_help_children(const AVClass * clazz, int flags);
其中clazz为任何自己声明的非class变量名
3.VERSION.h error: expected unqualified-id
这的确是很坑的一个问题,因为网上搜了一圈都没有找到相关的文章,坑了我两天的时间。
解决办法是把ffmpeg目录下面的VERSION.h删除掉就可以了
4.missing and no known rule to make it
build.gradle文件defaultConfig中添加如下代码
externalNativeBuild {
cmake {
cppFlags ""
abiFilters 'armeabi'//, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
}
其中abiFilers 是指你支持的so库。
5.undefined reference to “某某函数”
我们引入的这个第三方库是ffmpeg,该库需要用c编译器来编译,所以有一个
extern "C"
#include <jni.h>
#include <string>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
extern "C"
{
jstring
Java_com_example_lenovo_ffmpegdemo_FFmpegUtils_stringFromFF(JNIEnv *env, jobject ) {
char info[10000] = { 0 };
av_register_all();
sprintf(info, "%s\n", avcodec_configuration());
return env -> NewStringUTF(info);
}
jstring
Java_com_example_lenovo_ffmpegdemo_FFmpegUtils_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
}
应该改为
#include <jni.h>
#include <string>
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
jstring
Java_com_example_lenovo_ffmpegdemo_FFmpegUtils_stringFromFF(JNIEnv *env, jobject ) {
char info[10000] = { 0 };
av_register_all();
sprintf(info, "%s\n", avcodec_configuration());
return env -> NewStringUTF(info);
}
jstring
Java_com_example_lenovo_ffmpegdemo_FFmpegUtils_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}