现在的位置: 首页 > 综合 > 正文

在Android中通过jni方式使用编译好的FFmpeg库-Android中使用FFmpeg媒体库(二)

2013年10月01日 ⁄ 综合 ⁄ 共 3323字 ⁄ 字号 评论关闭

在继上篇在32位的Ubuntu 11.04中为Android NDK r6编译FFmpeg最新版0.8.1后,本人来给大家展示一下如何在Android中使用编译好的FFmpeg库。


整体调用逻辑为:
1 编译完ffmpeg库
2 使用jni方式撰写c代码,其中需要包含相应的ffmpeg的头文件
3 撰写相应的Android.mk文件,里面指定需要编译的c代码以及需要链接的动态库
4 执行ndk-build生成相应的jni库
5 创建andorid java程序,代码中loadLibrary相应ffmpeg库以及刚才生成的jni库
6 静态方法声明native函数,这些函数在jni写的c语言中都已经实现过



下面为步骤:
1 将在32位的Ubuntu 11.04中为Android NDK r6编译FFmpeg最新版0.8.1文中编译得到的libffmpeg.so文件拷贝到/root/develop/android-ndk-r6/platforms/android-8/arch-arm/usr/lib目录,如果使用的是Android2.3的话,还需有拷贝到/root/develop/android-ndk-r6/platforms/android-9/arch-arm/usr/lib目录。

2 进入Android NDK r6的samples目录,我们基于最简单的hello-jni来修改。由于我们在调用ffmpeg库方法时候,需要使用到他的头文件。这里我们将之前编译libffmpeg.so文件的所有代码拷贝到/root/develop/android-ndk-r6/samples/目录,并改目录名称为ffmpeg

3 修改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 <string.h>
#include <stdio.h>
#include <android/log.h>
#include <stdlib.h>
#include <jni.h>
//注意这里,需要在当前目录包含的时候能够找到libavcodec/avcodec.h文件
#include <ffmpeg/libavcodec/avcodec.h>
/* 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
 */
//这里的命名注意,相当于com.example.hellojni的HelloJni文件中的stringFromJNI函数
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    char str[25];
    sprintf(str, "%d", avcodec_version()); 
 
    return (*env)->NewStringUTF(env, str);
}

4 修改Android.mk文件

LOCAL_PATH
:= $(call my-dir)
 
include
$(CLEAR_VARS)
PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg
LOCAL_C_INCLUDES
+= $(PATH_TO_FFMPEG_SOURCE)
LOCAL_LDLIBS
:= -lffmpeg
LOCAL_MODULE   
:= hello-jni
LOCAL_SRC_FILES
:= hello-jni.c
include
$(BUILD_SHARED_LIBRARY)

注意LOCAL_LDLIBS := -lffmpeg是编译添加动态链接库文件。

5 进入命令行,在当前目录执行ndk-build

6 这时候会在/root/develop/android-ndk-r6/samples/hello-jni/libs/armeabi目录生成一个libhello-jni.so的动态链接库

7 为了后面的java程序能够loadLibrary,需要将之前生成的libffmpeg.so文件也拷贝到这个目录

8 修改HelloJni.java文件

package com.example.hellojni;
 
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
 
public class HelloJni extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView  tv = new TextView(this);
        tv.setText( "1111" );
        //System.out.println();
        setContentView(tv);
        tv.setText(String.valueOf(stringFromJNI()));
    }
 
    public native String  stringFromJNI();
 
    static {
          System.loadLibrary("ffmpeg");
        System.loadLibrary("hello-jni");
    }
}

执行后将看到如下图片:


本文源代码下载:
hello-jni
对应的编译完的libffmpeg.so文件下载
libffmpeg.so

ffmpeg文件夹比较大,这里不发了。可以从上篇文章找到。
tq09931兄的文章表示感谢。

http://tq09931.iteye.com/blog/1011895

http://code.google.com/p/aacplayer-android/

http://www.roman10.net/?p=394

https://github.com/halfninja/android-ffmpeg-x264

https://github.com/mconf/ffmpeg

https://github.com/havlenapetr/FFMpeg

主参考

http://tq09931.iteye.com/blog/1011895

https://github.com/havlenapetr/FFMpeg

https://github.com/churnlabs/android-ffmpeg-sample

转贴:http://doandroid.info/%E5%9C%A8android%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%BC%96%E8%AF%91%E5%A5%BD%E7%9A%84ffmpeg%E5%BA%93/

抱歉!评论已关闭.