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

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完