閱讀736 返回首頁    go 阿裏雲 go 技術社區[雲棲]


在Android中通過jni方式使用編譯好的FFmpeg庫-Android中使用FFmpeg媒體庫(二)

原文:https://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目錄。

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
 *
 *      https://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兄的文章表示感謝。

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

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

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

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

https://github.com/mconf/ffmpeg

https://github.com/havlenapetr/FFMpeg

主參考

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

https://github.com/havlenapetr/FFMpeg

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



最後更新:2017-04-02 06:51:59

  上一篇:go 一個fprintf的簡單封裝實例(vsprintf,va_start(),+va_arg(),+va_end()可變參數列表)
  下一篇:go Flex borderSkin繪自定義寬度邊框的實例