美文网首页
Android FFmpeg学习(二),第一次使用FFmpeg

Android FFmpeg学习(二),第一次使用FFmpeg

作者: 请叫我果爸 | 来源:发表于2020-06-02 09:40 被阅读0次

    前言

    ffmpeg已经编译成so了,我们怎么使用他呢。我简单使用ffmpeg,获取avcodec的版本号,来查看是否能正常使用so。

    使用

    Step.1 新建一个C++项目
    Step.2 将ffmpeg.so复制到libs目录下,将ffmpeg头文件复制到cpp目录下
    Step.3 修改CmakeList.txt文件
    cmake_minimum_required(VERSION 3.4.1)
    
    add_library( # Sets the name of the library.
            media-handle
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            audio_handler.cpp
            )
    
    add_library(ffmpeg
            SHARED
            IMPORTED)
    set_target_properties(ffmpeg
            PROPERTIES IMPORTED_LOCATION
            ../../../../libs/${ANDROID_ABI}/libffmpeg.so)
    add_library(mp3lame
            SHARED
            IMPORTED)
    set_target_properties(mp3lame
            PROPERTIES IMPORTED_LOCATION
            ../../../../libs/${ANDROID_ABI}/libmp3lame.so)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
    
    include_directories(.)
    include_directories(include)
    include_directories(include/armeabi-v7a)
    
    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)
    
    target_link_libraries( # Specifies the target library.
            media-handle
            ffmpeg
            mp3lame
    
            # Links the target library to the log library
            # included in the NDK.
            ${log-lib})
    
    Step.4 前面的步骤都完成了,可以创建一个FFmpegCmd.java类,通过JNI,与native方法进行通信。
    package com.explain.media.utils;
    
    public class FFmpegCmd {
        static {
            System.loadLibrary("media-handle");
            System.loadLibrary("ffmpeg");
            System.loadLibrary("mp3lame");
        }
        
        public static int getVersion(){
            return getAVCodecVersion();
        }
    
        private native static int getAVCodecVersion();
    }
    

    然后在CPP文件中写入

    #include <jni.h>
    #include <string>
    #include <android/log.h>
    
    #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"audio_handler", __VA_ARGS__)
    
    extern "C" {
    #include <include/libavcodec/avcodec.h>
    };
    
    extern "C"
    JNIEXPORT jint JNICALL
    Java_com_explain_media_utils_FFmpegCmd_getAVCodecVersion(JNIEnv *env, jclass clazz) {
        char str[25];
        int i;
        i = sprintf(str, "sshh%d", avcodec_version());
        LOGD("Output:avcodec_version = %d\n", i);
        return i;
    }
    

    结果

    调用一下FFmpegCmd.getVersion();打印一下avcodec的版本号,我们可以发现已经能正常使用so了。


    遇到的问题

    中间一直报错,找不到so,结果发现是gradle里面,忘记加入以下代码了。导致so没有被打进apk中。

        sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
            }
        }
    

    相关文章

      网友评论

          本文标题:Android FFmpeg学习(二),第一次使用FFmpeg

          本文链接:https://www.haomeiwen.com/subject/cwhjzhtx.html