NDK学习笔记(十四) 使用AVILib+window创建一个AVI视频播放器

2024-06-13 03:18

本文主要是介绍NDK学习笔记(十四) 使用AVILib+window创建一个AVI视频播放器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1.window api
  • 2.主要代码
  • 3.实现效果

1.window api

(1)从surface对象中检索原生window

从surface中检索对象window
ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface);

(2)获取原生window实例中的应用

void ANativeWindow_acquire(ANativeWindow* window);

(3)释放原生window引用

void ANativeWindow_release(ANativeWindow* window);

(4)检索原生window信息

宽度
int32_t ANativeWindow_getWidth(ANativeWindow* window);
宽度
int32_t ANativeWindow_getHeight(ANativeWindow* window);
像素格式
int32_t ANativeWindow_getFormat(ANativeWindow* window);

(5)设置原生window缓冲区的几何形状

int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,int32_t width, int32_t height, int32_t format);

(6)访问原生window缓冲区

int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,ARect* inOutDirtyBounds);

(7)释放原生window缓冲区

int32_t ANativeWindow_unlockAndPost(ANativeWindow* window);

2.主要代码

java

public class NativeWindowPlayerActivity extends AbstractPlayerActivity {private final AtomicBoolean isPlaying = new AtomicBoolean();private SurfaceHolder surfaceHolder;SurfaceView surfaceView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_native_window_player);surfaceView = findViewById(R.id.surface_view);surfaceHolder = surfaceView.getHolder();surfaceHolder.addCallback(callback);}@Overrideprotected void onStart() {super.onStart();int w = getWidth(avi);int h = getHeight(avi);//设置surfaceView的带大小,防止自动填充ViewGroup.LayoutParams viewGroup = surfaceView.getLayoutParams();viewGroup.width = w;viewGroup.height = h;surfaceView.setX(50);surfaceView.setY(50);}private final SurfaceHolder.Callback callback = new SurfaceHolder.Callback2() {@Overridepublic void surfaceRedrawNeeded(SurfaceHolder holder) {}@Overridepublic void surfaceCreated(SurfaceHolder holder) {isPlaying.set(true);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {isPlaying.set(true);new Thread(renderer).start();}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {isPlaying.set(false);}};private Runnable renderer = new Runnable() {@Overridepublic void run() {Surface surface = surfaceHolder.getSurface();init(avi, surface);long frameDelay = (long) (1000 / getFrameRate(avi));while (isPlaying.get()) {render(avi, surface);try {Thread.sleep(frameDelay);} catch (InterruptedException e) {break;}}}};/*** 初始化原生window** @param avi* @param surface*/private native static void init(long avi, Surface surface);/*** 渲染* @param avi* @param surface* @return*/private native static boolean render(long avi, Surface surface);
}

原生代码

#include "cn_study_aviplayer_NativeWindowPlayerActivity.h"extern "C" {
#include  "avilib/avilib.h"
}#include <android/native_window.h>
#include <android/native_window_jni.h>
#include "Common.h"extern "C"
JNIEXPORT void JNICALL
Java_cn_study_aviplayer_NativeWindowPlayerActivity_init(JNIEnv *env, jclass clazz, jlong avi,jobject surface) {//从surface中获取原生windowANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);if (0 == nativeWindow) {ThrowException(env, "java/io/RuntimeException", "不能都获取window");goto exit;}//设置buffer大小为avi视频帧的分辨率//如果和window的物理大小不一致//buffer会被缩放来匹配这个大小if (0 > ANativeWindow_setBuffersGeometry(nativeWindow, AVI_video_width((avi_t *) avi),AVI_video_height((avi_t *) avi),WINDOW_FORMAT_RGB_565)) {ThrowException(env, "java/io/RuntimeException", "不能够设置buffers");nativeWindow = 0;}exit:return;}extern "C"
JNIEXPORT jboolean JNICALL
Java_cn_study_aviplayer_NativeWindowPlayerActivity_render(JNIEnv *env, jclass clazz, jlong avi,jobject surface) {jboolean isFrameRead = JNI_FALSE;long frameSize = 0;int keyFrame = 0;ANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);if (0 == nativeWindow) {ThrowException(env, "java/io/RuntimeException", "不能够获取window");goto exit;}//锁定原生window并访问原始bufferANativeWindow_Buffer windowBuffer;if (0 > ANativeWindow_lock(nativeWindow, &windowBuffer, 0)) {ThrowException(env, "java/io/RuntimeException", "不能锁住window");goto release;}//将avi帧的比特流读至原始缓冲区frameSize = AVI_read_frame((avi_t *) avi, (char *) windowBuffer.bits, &keyFrame);//是否读取成功if (0 < frameSize) {isFrameRead = JNI_TRUE;}//解锁并且输出缓冲区来显示if (0 > ANativeWindow_unlockAndPost(nativeWindow)) {ThrowException(env, "java/io/RuntimeException", "不能够解锁window");goto release;}release:ANativeWindow_release(nativeWindow);nativeWindow = 0;exit:return isFrameRead;}

配置cmake
jnigraphics与android会冲突,所以加了前缀-l。

cmake_minimum_required(VERSION 3.4.1)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).native-lib.cppCommon.cppcn_study_aviplayer_BitmapPlayerActivity.cppcn_study_aviplayer_OpenGLPlayerActivity.cppcn_study_aviplayer_NativeWindowPlayerActivity.cpp)
#添加子目录
add_subdirectory(avilib)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)find_library(GLESv2-lib-lGLESv2)find_library(android-lib-landroid)target_link_libraries( # Specifies the target library.native-lib# Links the target library to the log library# included in the NDK.-ljnigraphics#开启jnigraphics${android-lib}#使用window${log-lib}${GLESv2-lib}avi-lib)

3.实现效果

在这里插入图片描述

这篇关于NDK学习笔记(十四) 使用AVILib+window创建一个AVI视频播放器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1056125

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr