使用Android Studio编写第一个ffmpeg程序
来源:互联网 发布:苹果公司营销策略数据 编辑:程序博客网 时间:2024/05/29 10:58
使用Android Studio编写第一个ffmpeg程序
所需材料
1、android studio 2.3
2、android-ndk-r13
3、ffmpeg编译结果(include,libs)
本文实例中需要使用到编译的ffmpeg头文件和静态库。
本文使用Android Studio2.3 来开发,主要是考虑到Android Studio对NDK开发已经支持得很好了。
Android Sutdio 既可以支持ndk-build构建,又支持新的CMake构建,而且代码提示、单步调试等功能也大大提高了开发效率。
1、创建一个NDK工程
打开Android Studio,新建一个工程,勾选“Include C++support”,使工程支持C++开发。如下图:
下一步,
下一步,
下一步,
C++编译选项采用默认的就可以了,以后可以在“build.gradle”里面进行更改。
完成工程的创建。
2、安装CMake
新建工程后,出现如下错误,原因是:新版本的Android Studio默认使用CMake进行构建,与以往使用mk和ndk-build不一样,需要下载CMake工具才行。
点击上面的链接“Install CMake and sync project”,自动下载所需的组件。
完成成后,点击“Finish”即可。
3、配置NDK
NDK没配置好的话,会提示上面的错误。
使用CMake构建C++程序的话,需要使用NDK>=12r,如果没有的话,请自行百度搜索下载。
打开工程中的“local.properties”文件,追加一行代码“ndk.dir=NDK的目录”,如下所示:
sync一下,即可解决NDK编译环境的问题。
4、导入ffmpeg编译结果
将前面编译的ffmpeg结果拷贝到工程目录中,路径“app/src/main/cpp/ffmpeg”,结构如下图所示:
其中“native-lib.cpp”是这个IDE自动生成的源文件,“CMakeLists.txt”是这个IED自动生成的构建脚本文件。
# For more information about using CMake with Android Studio, read the
# documentation: https:
//d.android.com/studio/projects/add-native-code.html
?
# Sets the minimum version of CMake required to build the
native
library.
?
cmake_minimum_required(VERSION
3.4
.
1
)
?
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them
for
you.
# Gradle automatically packages shared libraries with your APK.
?
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 )
?
#增加头文件搜索目录
include_directories(src/main/cpp/ffmpeg/include/)
?
# Searches
for
a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
#
default
, you only need to specify the name of the
public
NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
?
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 )
?
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in
this
# build script, prebuilt third-party libraries, or system libraries.
?
target_link_libraries( # Specifies the target library.
native
-lib
?
#增加ffmpeg库,注意顺序(具体看lib/pkgconfig的依赖关系)
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavfilter.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavformat.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavcodec.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswresample.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswscale.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavutil.a
?
#增加ffmpeg所需要的依赖库
-lm -lz
?
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
- 指定ffmpeg头文件的搜索路径;
- 将ffmpeg的静态库添加到构建中。
6、编写代码
#include
#include
"libavcodec/avcodec.h"
extern
"C"
JNIEXPORT jstring JNICALL
Java_cn_bassy_ffmpeg01_MainActivity_stringFromJNI(JNIEnv* env, jobject
/* this */
) {
return
env->NewStringUTF(avcodec_configuration());
}
Android Studio 有代码提示功能,鼠标放在“”函数上面,可以看到相关注释。
如果没有显示,请进入Settings配置,如下图所示。
Java代码就用IDE自动生成的就可以了。
7、编译程序
编译程序很简单,Android Studio已经集成了NDK开发,只需要点击“Run App”就可以了。
当然,肯定还会有很多错误的,所以下面给出了常见错误及解决方法。
8、错误问题及解决方法
注意,编译ffmpeg时所指定的Android Platform版本,与使用ffmpeg时指定的minSdkVersion有很大的关系,大部分错误都是因为minSdkVersion大于编译ffmpeg时指定的Android Platform版本所导致的!!
1、ffmpeg函数不能被识别
编译是出现" undefined reference to 'avcodec_register_all()"和“Error:(15) undefined reference to 'avcodec_configuration()”,错误如下:
Error:(
15
) undefined reference to
'avcodec_register_all()'
?
Error:(
15
) undefined reference to
'avcodec_configuration()'
#include
#include
?
#ifdef __cplusplus
extern
"C"
{
#endif
?
#include
"libavcodec/avcodec.h"
?
?
JNIEXPORT jstring JNICALL
Java_cn_bassy_ffmpeg01_MainActivity_stringFromJNI(JNIEnv* env, jobject
/* this */
) {
return
env->NewStringUTF(avcodec_configuration());
}
?
#ifdef __cplusplus
#endif
Error:error: undefined reference to
'av_log'
Error:error: undefined reference to
'av_log'
Error:error: undefined reference to
'av_log'
Error:error: undefined reference to
'av_log'
Error:error: undefined reference to
'av_get_bytes_per_sample'
Error:error: undefined reference to
'av_crc'
Error:error: undefined reference to
'av_crc'
Error:error: undefined reference to
'av_crc'
target_link_libraries( # Specifies the target library.
native
-lib
#增加ffmpeg库,注意顺序(具体看lib/pkgconfig的依赖关系)
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavfilter.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavformat.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavcodec.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswresample.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswscale.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavutil.a
#增加ffmpeg所需要的依赖库
-lm -lz
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
defaultConfig {
applicationId
"cn.bassy.ffmpeg01"
minSdkVersion
16
targetSdkVersion
25
versionCode
1
versionName
"1.0"
}
APP_STL := gnustl_static
APP_PLATFORM := android-
17
APP_ABI := armeabi
libavutil/common.h:
30
:
2
: error: missing -D__STDC_CONSTANT_MACROS
libavutil/common.h:
205
:
28
: error: use of undeclared identifier
'UINT64_C'
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
# include
"stdint.h"
#endif
?
?
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
9、运行效果
- 使用Android Studio编写第一个ffmpeg程序
- 12、ffmpeg笔记-使用Android Studio编写第一个ffmpeg程序
- 编写第一个Android程序
- 编写 Android第一个程序 Hello, Android!
- 编写第一个Android程序------HelloWorld
- 编写第一个Android运行程序
- 使用Playground编写第一个Swift程序
- 使用Eclipse编写第一个Hibernate程序
- 使用AndroidStudio编写第一个JNI程序
- Android Studio第一个jni程序
- Android Studio运行第一个NDK程序
- Android Studio 第一个JNI程序
- 使用kotlin编写Android第一个Activity
- 3.1 用Android Studio编写第一个应用程序Hello World
- Android JNI编程(七)——使用AndroidStudio编写第一个JNI程序
- 编写第一个J2ME程序
- 编写第一个WINDOWS程序
- 编写第一个NDK程序
- 文件权限限制解读
- 谈冒烟测试
- maven搭建
- java 成神之路
- python变量及字符编码
- 使用Android Studio编写第一个ffmpeg程序
- Java多线程与并发库高级应用以及设计模式的实战应用
- 说说常见的http状态码
- 笨方法学Python 习题 37: 复习各种符号
- 小程序干掉APP这个结论或许下的太早 但小程序有许多可取之处
- servChannel.register()源码分析
- hdu 6090-Rikka with Graph
- 关于77页读写string对象编程错误
- 解决IE8 内置JSON.stringify,中文变unicode的问题