FFMPEG for Android(二)

  本文是关于在Android Studio下配置FFMPEG的具体流程,配置前需要准备好FFMPEG头文件和so文件,可参看FFMPEG for Android(一)编译FFMPEG头文件和so文件。

环境

  • Win10
  • Android Studio 3.0.1

第一步 新建项目

  打开Android Studio 新建一个项目,需要勾选Include C++ support选项。
这里写图片描述
这里写图片描述

第二步 引入头文件和so文件

  将生成的include和lib
这里写图片描述
  放入到如图project–>app–>libs下
这里写图片描述
  放入后文件的结构
这里写图片描述

第三步 修改CMakeLists.txt

  两处有start-end 为自己添加的,其他的为自动生成的。

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

#-------------------------------start-------------------------------------
include_directories(libs/include)

set(distribution_DIR ../../../../libs)

add_library( avcodec
             SHARED
             IMPORTED)
set_target_properties( avcodec
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/lib/libavcodec.so)

add_library( avfilter
             SHARED
             IMPORTED)
set_target_properties( avfilter
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/lib/libavfilter.so)

add_library( avformat
             SHARED
             IMPORTED)
set_target_properties( avformat
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/lib/libavformat.so)

add_library( avutil
             SHARED
             IMPORTED)
set_target_properties( avutil
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/lib/libavutil.so)

add_library( swresample
             SHARED
             IMPORTED)
set_target_properties( swresample
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/lib/libswresample.so)

add_library( swscale
             SHARED
             IMPORTED)
set_target_properties( swscale
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/lib/libswscale.so)



#----------------------end-----------------------------------


# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.


target_link_libraries( # Specifies the target library.
                       native-lib
                       #------------------start-----------------------------
                                              avcodec
                                              avfilter
                                              avformat
                                              avutil
                                              swresample
                                              swscale
                       #-------------------------end----------------------------

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

第四步 修改build.gradle

这里写图片描述

第五步 测试

  测试程序可以参考http://blog.csdn.net/leixiaohua1020/article/details/47008825/
1. 将有关C语言的程序代码写入cpp–>native-lib.cpp中
2. 将java程序写入com包下相关activity中
4. 通过public native String avformatinfo()(avformatinfo为函数名)将二者关联起来。
这里写图片描述
例子程序
MainActivity.java

package com.ffmpeg.ffmpegapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener{

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("avcodec");
        System.loadLibrary("avfilter");
        System.loadLibrary("avformat");
        System.loadLibrary("avutil");
        System.loadLibrary("swresample");
        System.loadLibrary("swscale");
    }

    private Button button;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.format);
        text = findViewById(R.id.show);
        button.setOnClickListener(this);


    }
    public native String avformatinfo();

    @Override
    public void onClick(View v) {

        text.setText(avformatinfo());
    }
}

native-lib.cpp

#include <jni.h>
#include <string>

#include <android/log.h>

extern "C" {
//编码
#include "libavcodec/avcodec.h"
//封装格式处理
#include "libavformat/avformat.h"
//像素处理
#include "libswscale/swscale.h"
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_ffmpeg_ffmpegapplication_MainActivity_avformatinfo(JNIEnv *env, jobject instance) {

    char info[40000] = { 0 };

    av_register_all();

    AVInputFormat *if_temp = av_iformat_next(NULL);
    AVOutputFormat *of_temp = av_oformat_next(NULL);
    //Input
    while(if_temp!=NULL){
        sprintf(info, "%s[In ][%10s]\n", info, if_temp->name);
        if_temp=if_temp->next;
    }
    //Output
    while (of_temp != NULL){
        sprintf(info, "%s[Out][%10s]\n", info, of_temp->name);
        of_temp = of_temp->next;
    }
    //LOGE("%s", info);
    //return (*env)->NewStringUTF(env, info);


    return env->NewStringUTF(info);
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <Button
            android:id="@+id/format"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="format"
            />
        <TextView
            android:id="@+id/show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

  很多不完善的地方,以后再更改。
  
参考:https://www.cnblogs.com/CoderTian/p/6651343.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值