工程目录:
FFmpeg4AndroidActivity.java:
package cn.dennishucd;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class FFmpeg4AndroidActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)this.findViewById(R.id.textview_hello);
FFmpegNative ffmpeg = new FFmpegNative();
int codecID = 28;
int res = ffmpeg.avcodec_find_decoder(codecID);
if (res ==0) {
tv.setText("Success!");
}
else {
tv.setText("Failed!");
}
}
}
FFmpegNative.java:
package cn.dennishucd;
public class FFmpegNative {
static {
System.loadLibrary("avutil-52");
System.loadLibrary("avcodec-55");
System.loadLibrary("swresample-0");
System.loadLibrary("avformat-55");
System.loadLibrary("swscale-2");
System.loadLibrary("avfilter-4");
System.loadLibrary("avdevice-55");
System.loadLibrary("ffmpeg_codec");
}
public native int avcodec_find_decoder(int codecID);
}
prebuilt文件夹里的为编译好的ffmpeg动态库
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := avcodec-55-prebuilt
LOCAL_SRC_FILES := prebuilt/libavcodec-55.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avdevice-55-prebuilt
LOCAL_SRC_FILES := prebuilt/libavdevice-55.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avfilter-4-prebuilt
LOCAL_SRC_FILES := prebuilt/libavfilter-4.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avformat-55-prebuilt
LOCAL_SRC_FILES := prebuilt/libavformat-55.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avutil-52-prebuilt
LOCAL_SRC_FILES := prebuilt/libavutil-52.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avswresample-0-prebuilt
LOCAL_SRC_FILES := prebuilt/libswresample-0.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := swscale-2-prebuilt
LOCAL_SRC_FILES := prebuilt/libswscale-2.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg_codec
LOCAL_SRC_FILES := cn_dennishucd_FFmpegNative.c
LOCAL_LDLIBS := -llog -ljnigraphics -lz -landroid
LOCAL_SHARED_LIBRARIES := avcodec-55-prebuilt avdevice-55-prebuilt avfilter-4-prebuilt avformat-55-prebuilt avutil-52-prebuilt
include $(BUILD_SHARED_LIBRARY)
Application.mk:
APP_ABI := armeabi
#APP_ABI := armeabi-v7a
APP_PLATFORM := android-10
cn_dennishucd_FFmpegNative.c:
#include <math.h>
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include "cn_dennishucd_FFmpegNative.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_dennishucd_FFmpegNative
* Method: avcodec_find_decoder
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_cn_dennishucd_FFmpegNative_avcodec_1find_1decoder
(JNIEnv *env, jobject obj, jint codecID)
{
AVCodec *codec = NULL;
/* register all formats and codecs */
av_register_all();
codec = avcodec_find_decoder(codecID);
if (codec != NULL)
{
return 0;
}
else
{
return -1;
}
}
#ifdef __cplusplus
}
#endif
cn_dennishucd_FFmpegNative.h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class cn_dennishucd_FFmpegNative */
#ifndef _Included_cn_dennishucd_FFmpegNative
#define _Included_cn_dennishucd_FFmpegNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_dennishucd_FFmpegNative
* Method: avcodec_find_decoder
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_cn_dennishucd_FFmpegNative_avcodec_1find_1decoder
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textview_hello"
android:text="@string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.dennishucd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".FFmpeg4AndroidActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>