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

相关文章

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案