本文参考:https://blog.youkuaiyun.com/JohanMan/article/details/83091706
#include <jni.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <android/log.h>
// Android 打印 Log
#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR, "player", FORMAT, ##__VA_ARGS__);
#ifdef __cplusplus
extern "C" {
#endif
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
void Java_com_xupt_will_ffmpegtest_FFMPEGPlayer_play(JNIEnv *env,jobject thiz,jstring path_,jobject surface){
// 记录结果
int result;
// R1 Java String -> C String
const char *path = env->GetStringUTFChars(path_,0);
//注册FFMPEG组件
av_register_all();
// R2 初始化 AVFormatContext 上下文
AVFormatContext *formatContext = avformat_alloc_context();
// 打开视频文件
result = avformat_open_input(&formatContext,path,NULL,NULL);
// 如果视频打开错误,退出
if(result < 0){
LOGE("FFMPEG Player Error: Can not open video file");
return;
}
// 查看文件的视频流信息
result = avformat_find_stream_info(formatContext,NULL);
// 如果查看视频流信息失败,退出
if(result < 0){
LOGE("FFMPEG Play Error: Can not find video file stream info");
return;
}
// 查找视频编码器
int video_stream_index = -1;
for(int i = 0 ; i < formatContext->nb_streams ; i++){
if(formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
video_stream_index = i;
break;
}
}
// 如果没有找到视频流,退出
if(video_stream_index == -1){
LOGE("FFMPEG Play Error: Can not find video codec");
return;
}
//初始化视频编码器上下文
AVCodecContext *video_codec_context = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(video_codec_context, formatContext->streams[video_stream_index]->codec);
//初始化视频编码器
}
#ifdef __cplusplus
}
#endif