HelloWorld App of ffmpeg JNI
(1)、用eclipse导入NDK 示例Samples中的helloJNI工程
(2)、将编译的ffmpeg源代码复制到工程目录中的jni目录中
(3)、修改HelloJni.java代码为:
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.example.hellojni; import android.app.Activity;import android.widget.TextView;import android.os.Bundle; public class HelloJni extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Create a TextView and set its content. * the text is retrieved by calling a native * function. */ TextView tv = new TextView(this); tv.setText( "1111" ); //System.out.println(); setContentView(tv); tv.setText(String.valueOf(stringFromJNI())); } /* A native method that is implemented by the * 'hello-jni' native library, which is packaged * with this application. */ public native String stringFromJNI(); /* This is another native method declaration that is *not* * implemented by 'hello-jni'. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* this is used to load the 'hello-jni' library on application * startup. The library has already been unpacked into * /data/data/com.example.HelloJni/lib/libhello-jni.so at * installation time by the package manager. */ static { System.loadLibrary("ffmpeg"); System.loadLibrary("hello-jni"); }}
(4)修改jni目录中:hello-jni.c文件
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include#include #include #include #include #include /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java */jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ){ char str[25]; sprintf(str, "%d", avcodec_version()); return (*env)->NewStringUTF(env, str);}
(5)jni目录中添加文件:Android.mk
# Copyright (C) 2009 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS)PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpegLOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)LOCAL_LDLIBS := -lffmpegLOCAL_MODULE := hello-jniLOCAL_SRC_FILES := hello-jni.c include $(BUILD_SHARED_LIBRARY)
(6)、将编译好的libffmpeg.so库文件复制到NDK的工程对应的平台的库目录中,比如说:工程对应的Android平台为android 4.0,则将so文件复制到:android-ndk-r8b\platforms\android-14\arch-arm\usr\lib中。
(7)、在工程根目录运行:ndk-build编译工程jni库,前提是NDK目录已经加到系统Path环境变量中。
(8)、生成工程,并将libffmpeg.so文件复制到工程的jni目录中。
(9)、打包android工程,生成apk。
(10)、好了,完满了,可以虚拟机,真机,测试程序了!下面是效果图额(*^_^*)
Android status bar .;| I=I-------------------------------------------------hellojni-------------------------------------------------3743534<<-- ^_^ ==