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

原文:http://doandroid.info/?p=471

在继上篇在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目录。注:(每个平台都需要拷贝一份so)

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

3 修改hello-jni.c文件

[java]  view plain copy
  1. /* 
  2.  * Copyright (C) 2009 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  * 
  16.  */  
  17. #include <string.h>  
  18. #include <stdio.h>  
  19. #include <android/log.h>  
  20. #include <stdlib.h>  
  21. #include <jni.h>  
  22. //注意这里,需要在当前目录包含的时候能够找到libavcodec/avcodec.h文件  
  23. #include <ffmpeg/libavcodec/avcodec.h>  
  24. /* This is a trivial JNI example where we use a native method 
  25.  * to return a new VM String. See the corresponding Java source 
  26.  * file located at: 
  27.  * 
  28.  *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java 
  29.  */  
  30. //这里的命名注意,相当于com.example.hellojni的HelloJni文件中的stringFromJNI函数  
  31. jstring  
  32. Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,  
  33.                                                   jobject thiz )  
  34. {  
  35.     char str[25];  
  36.     sprintf(str, "%d", avcodec_version());   
  37.    
  38.     return (*env)->NewStringUTF(env, str);  
  39. }  

4 修改Android.mk文件

[java]  view plain copy
  1. LOCAL_PATH := $(call my-dir)  
  2.    
  3. include $(CLEAR_VARS)  
  4. PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg  
  5. LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)  
  6. LOCAL_LDLIBS := -lffmpeg  
  7. LOCAL_MODULE    := hello-jni  
  8. LOCAL_SRC_FILES := hello-jni.c  
  9. 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文件

[java]  view plain copy
  1. package com.example.hellojni;  
  2.    
  3. import android.app.Activity;  
  4. import android.widget.TextView;  
  5. import android.os.Bundle;  
  6.    
  7. public class HelloJni extends Activity  
  8. {  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState)  
  11.     {  
  12.         super.onCreate(savedInstanceState);  
  13.         TextView  tv = new TextView(this);  
  14.         tv.setText( "1111" );  
  15.         //System.out.println();  
  16.         setContentView(tv);  
  17.         tv.setText(String.valueOf(stringFromJNI()));  
  18.     }  
  19.    
  20.     public native String  stringFromJNI();  
  21.    
  22.     static {  
  23.           System.loadLibrary("ffmpeg");  
  24.         System.loadLibrary("hello-jni");  
  25.     }  
  26. }  

执行后将看到如下图片:

本文源代码下载:
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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值