• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

[FFmpeg万能音频播放器]android CMake项目准备阶段(一)

互联网 diligentman 4天前 9次浏览

1.新建c++项目

2.配置NDK路径    这里建议配置NDK版本为:android-ndk-r15c

3.在SNKtool下安装CMake

4.新建Module 并且添加Module (这个module主要用来加载库与写jni)      new ->Module     FIle ->ProjectStructure

[FFmpeg万能音频播放器]android CMake项目准备阶段(一)

5.在新建的module中写Demo    也就是jni的java通信类

package com.example.myplayer;

public class Demo {

    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("avutil");
        System.loadLibrary("swresample");
        System.loadLibrary("avcodec");
        System.loadLibrary("avformat");
        System.loadLibrary("swscale");
        System.loadLibrary("postproc");
        System.loadLibrary("avfilter");
        System.loadLibrary("avdevice");
    }

    public native String stringFromJNI();
    
    public native String showDetial();
}

6.新建的module中 cpp文件夹下 将FFmpeg的头文件添加进去  

[FFmpeg万能音频播放器]android CMake项目准备阶段(一)

7.新建的module中 在main路劲下新建jniLibs目录用来存放FFmpeg .so文件库

这里如果没有苦可以参考这篇https://blog.csdn.net/we1less/article/details/109039079

[FFmpeg万能音频播放器]android CMake项目准备阶段(一)

8.新建的module中  编写build.gradle文件

在    android {     defaultConfig {   下添加

externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                abiFilters "x86"
            }
        }

在    android {  下添加

externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
            version "3.10.2"
        }
    }


sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/jniLibs']
        }
    }

9.新建的module中  编写CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

include_directories(src/main/cpp/include)


add_library(
             native-lib
             SHARED
           ${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp)


add_library( avcodec
        SHARED
        IMPORTED)

set_target_properties( avcodec
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavcodec.so)

add_library( avdevice
             SHARED
             IMPORTED)

set_target_properties( avdevice
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavdevice.so)

add_library( avfilter
             SHARED
             IMPORTED)

set_target_properties( avfilter
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavfilter.so)

add_library( avformat
             SHARED
             IMPORTED)

set_target_properties( avformat
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavformat.so)

add_library( avutil
             SHARED
             IMPORTED)

set_target_properties( avutil
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavutil.so)

add_library( swresample
             SHARED
             IMPORTED)

set_target_properties( swresample
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswresample.so)

add_library( swscale
             SHARED
             IMPORTED)

set_target_properties( swscale
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswscale.so)

add_library( postproc
             SHARED
             IMPORTED)

set_target_properties( postproc
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libpostproc.so)

target_link_libraries(
                        native-lib
                        avcodec
                        avdevice
                        avfilter
                        avformat
                        avutil
                        swresample
                        swscale
                        postproc
                        log )

10.编写cpp文件

#include <jni.h>
#include <string>
#include <android/log.h>

extern "C"{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}


#define LOG_TAG "godv"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)


extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myplayer_Demo_stringFromJNI(JNIEnv *env, jobject thiz) {
    std::string hello = "Hello godv";

    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myplayer_Demo_showDetial(JNIEnv *env, jobject thiz) {
    av_register_all();
    AVCodec *c_temp = av_codec_next(NULL);
    while (c_temp != NULL)
    {
        switch (c_temp->type)
        {
            case AVMEDIA_TYPE_VIDEO:
                LOGD("[Video]:%s", c_temp->name);
                break;
            case AVMEDIA_TYPE_AUDIO:
                LOGD("[Audio]:%s", c_temp->name);
                break;
            default:
                LOGD("[Other]:%s", c_temp->name);
                break;
        }
        c_temp = c_temp->next;
    }
    std::string hello = "Hello godv";
    return env->NewStringUTF(hello.c_str());
}

11.在mainactivity中调用

package com.example.godvmusic;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.myplayer.Demo;

public class MainActivity extends AppCompatActivity {

    private Demo mDemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDemo = new Demo();
    }

    public void run(View view){
        mDemo.showDetial();
    }
}

12.最后附上整个项目的结构图

[FFmpeg万能音频播放器]android CMake项目准备阶段(一)

 


喜欢 (0)