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

   日期:2020-10-16     浏览:130    评论:0    
核心提示:1.新建c++项目2.配置NDK路径 这里建议配置NDK版本为:android-ndk-r15c3.在SNKtool下安装CMake4.新建Module 并且添加Module (这个module主要用来加载库与写jni) new ->Module FIle ->ProjectStructure5.在新建的module中写Demo 也就是jni的java通信类package com.example.myplayer;public clas...

1.新建c++项目

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

3.在SNKtool下安装CMake

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

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的头文件添加进去  

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

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

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.最后附上整个项目的结构图

 

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服