Skip to content

Commit

Permalink
添加接口功能
Browse files Browse the repository at this point in the history
1.添加背景图渲染
2.添加播放次数逻辑
  • Loading branch information
zengjiale2 committed Oct 12, 2022
1 parent 13dbfbd commit 67f321c
Show file tree
Hide file tree
Showing 21 changed files with 372 additions and 92 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ interface IEvaAnimView {
fun isRunning(): Boolean
//循环播放
fun setLoop(playLoop: Int)
//设置背景图
fun setBgImage(bg: Bitmap)
...
}
```
Expand Down
4 changes: 4 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ interface IEvaAnimView {
fun stopPlay()
//是否正在运行
fun isRunning(): Boolean
//循环播放
fun setLoop(playLoop: Int)
//设置背景图
fun setBgImage(bg: Bitmap)
...
}
```
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/yy/yyeva/player/EvaDemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.os.Looper
import android.view.View
import android.widget.Toast
import com.yy.yyeva.EvaAnimConfig
import com.yy.yyeva.player.R
import com.yy.yyeva.inter.IEvaAnimListener
import com.yy.yyeva.inter.IEvaFetchResource
import com.yy.yyeva.inter.OnEvaResourceClickListener
Expand Down
114 changes: 59 additions & 55 deletions app/src/main/java/com/yy/yyeva/player/EvaKeyDemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import android.os.Looper
import android.view.View
import android.widget.Toast
import com.yy.yyeva.EvaAnimConfig
import com.yy.yyeva.player.R
import com.yy.yyeva.inter.IEvaAnimListener
import com.yy.yyeva.inter.IEvaFetchResource
import com.yy.yyeva.inter.OnEvaResourceClickListener
import com.yy.yyeva.mix.EvaResource
import com.yy.yyeva.util.ELog
Expand All @@ -22,6 +20,7 @@ import java.io.File
import java.util.*
import android.text.TextPaint
import android.util.Log
import com.yy.yyeva.inter.IEvaFetchResource
import com.yy.yyeva.view.EvaAnimViewV3
import kotlinx.android.synthetic.main.activity_anim_simple_demo_p.*
import kotlin.math.abs
Expand Down Expand Up @@ -72,59 +71,59 @@ class EvaKeyDemoActivity : Activity(), IEvaAnimListener {
/**
* 注册资源获取类
*/
// animView.setFetchResource(object : IEvaFetchResource {
// /**
// * 获取图片资源
// * 无论图片是否获取成功都必须回调 result 否则会无限等待资源
// */
// override fun setImage(resource: EvaResource, result: (Bitmap?) -> Unit) {
// /**
// * tag是素材中的一个标记,在制作素材时定义
// * 解析时由业务读取tag决定需要播放的内容是什么
// * 比如:一个素材里需要显示多个头像,则需要定义多个不同的tag,表示不同位置,需要显示不同的头像,文字类似
// */
// val tag = resource.tag
// if (tag == "anchor_avatar1") { // 此tag是已经写入到动画配置中的tag
// val drawableId = R.drawable.ball_1
// val options = BitmapFactory.Options()
// options.inScaled = false
// result(BitmapFactory.decodeResource(resources, drawableId, options))
// } else if (tag == "anchor_avatar2") { // 此tag是已经写入到动画配置中的tag
// val drawableId = R.drawable.ball_2
// val options = BitmapFactory.Options()
// options.inScaled = false
// result(BitmapFactory.decodeResource(resources, drawableId, options))
// } else if (tag == "anchor_avatar3") { // 此tag是已经写入到动画配置中的tag
// val drawableId = R.drawable.ball_3
// val options = BitmapFactory.Options()
// options.inScaled = false
// result(BitmapFactory.decodeResource(resources, drawableId, options))
// } else {
// result(null)
// }
// }
//
// /**
// * 获取文字资源
// */
// override fun setText(resource: EvaResource, result: (String?) -> Unit) {
// val tag = resource.tag
// if (tag == "anchor_nick") { // 此tag是已经写入到动画配置中的tag
// result("USERNICK")
// } else {
// result(null)
// }
// }
//
// /**
// * 播放完毕后的资源回收
// */
// override fun releaseSrc(resources: List<EvaResource>) {
// resources.forEach {
// it.bitmap?.recycle()
// }
// }
// })
animView.setFetchResource(object : IEvaFetchResource {
/**
* 获取图片资源z
* 无论图片是否获取成功都必须回调 result 否则会无限等待资源
*/
override fun setImage(resource: EvaResource, result: (Bitmap?) -> Unit) {
/**
* tag是素材中的一个标记,在制作素材时定义
* 解析时由业务读取tag决定需要播放的内容是什么
* 比如:一个素材里需要显示多个头像,则需要定义多个不同的tag,表示不同位置,需要显示不同的头像,文字类似
*/
val tag = resource.tag
if (tag == "anchor_avatar1") { // 此tag是已经写入到动画配置中的tag
val drawableId = R.drawable.ball_1
val options = BitmapFactory.Options()
options.inScaled = false
result(BitmapFactory.decodeResource(resources, drawableId, options))
} else if (tag == "anchor_avatar2") { // 此tag是已经写入到动画配置中的tag
val drawableId = R.drawable.ball_2
val options = BitmapFactory.Options()
options.inScaled = false
result(BitmapFactory.decodeResource(resources, drawableId, options))
} else if (tag == "anchor_avatar3") { // 此tag是已经写入到动画配置中的tag
val drawableId = R.drawable.ball_3
val options = BitmapFactory.Options()
options.inScaled = false
result(BitmapFactory.decodeResource(resources, drawableId, options))
} else {
result(null)
}
}

/**
* 获取文字资源
*/
override fun setText(resource: EvaResource, result: (String?) -> Unit) {
val tag = resource.tag
if (tag == "anchor_nick") { // 此tag是已经写入到动画配置中的tag
result("USERNICK")
} else {
result(null)
}
}

/**
* 播放完毕后的资源回收
*/
override fun releaseSrc(resources: List<EvaResource>) {
resources.forEach {
it.bitmap?.recycle()
}
}
})

// 注册点击事件监听
animView.setOnResourceClickListener(object : OnEvaResourceClickListener {
Expand All @@ -139,6 +138,9 @@ class EvaKeyDemoActivity : Activity(), IEvaAnimListener {

// 注册动画监听
animView.setAnimListener(this)
//设置背景图
val img = BitmapFactory.decodeResource(resources, R.drawable.bg)
animView.setBgImage(img)
/**
* 开始播放主流程
* 主要流程都是对AnimViewV3的操作,内部是集成TextureView
Expand Down Expand Up @@ -170,6 +172,8 @@ class EvaKeyDemoActivity : Activity(), IEvaAnimListener {
ELog.e(TAG, "${videoInfo.fileName} is not exist")
return@Thread
}
//循环三次
animView.setLoop(3)
animView.startPlay(file)
// val md5 = FileUtil.getFileMD5(file)
// if (videoInfo.md5 == md5) {
Expand Down
Binary file added app/src/main/res/drawable-xhdpi/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions yyevac/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_library( # Sets the name of the library.
src/main/cpp/egl/eglcore.cpp
src/main/cpp/engine/yuvrender.cpp
src/main/cpp/engine/render.cpp
src/main/cpp/engine/bgrender.cpp
src/main/cpp/engine/rendercontroller.cpp
src/main/cpp/engine/evaengine.cpp
src/main/cpp/util/shaderutil.cpp
Expand Down
100 changes: 100 additions & 0 deletions yyevac/src/main/cpp/engine/bgrender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// Created by zengjiale on 2022/10/11.
//

#include "bgrender.h"

BgRender::BgRender() {
initRender();
}

BgRender::~BgRender() {
vertexArray = nullptr;
rgbaArray = nullptr;
}

void BgRender::setBgImage(unsigned char *bitmap, AndroidBitmapInfo *bitmapInfo) {
textureId = TextureLoadUtil::loadTexture(bitmap, bitmapInfo);
}


void BgRender::initRender() {
char VERTEX_SHADER[] = "attribute vec4 vPosition;\n"
"attribute vec4 vTexCoordinate;\n"
"varying vec2 v_TexCoordinate;\n"
"\n"
"void main() {\n"
" v_TexCoordinate = vec2(vTexCoordinate.x, vTexCoordinate.y);\n"
" gl_Position = vPosition;\n"
"}";

char FRAGMENT_SHADER[] = "precision mediump float;\n"
"uniform sampler2D texture;\n"
"varying vec2 v_TexCoordinate;\n"
"\n"
"void main () {\n"
" gl_FragColor = texture2D(texture, v_TexCoordinate);\n"
// " gl_FragColor = vec4(1.0,0.2,0.5,1.0);\n"
"}";
shaderProgram = ShaderUtil::createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
uTextureLocation = glGetUniformLocation(shaderProgram, "texture");
positionLocation = glGetAttribLocation(shaderProgram, "vPosition");
textureLocation = glGetAttribLocation(shaderProgram, "vTexCoordinate");
}

void BgRender::renderFrame() {
if (surfaceSizeChanged && surfaceWidth > 0 && surfaceHeight > 0) {
surfaceSizeChanged = false;
glViewport(0, 0, surfaceWidth, surfaceHeight);
}
draw();
}

void BgRender::clearFrame() {
glClearColor(0, 0, 0, 0);

glClear(GL_COLOR_BUFFER_BIT);
// glClearColor(1.0, 0, 0.5, 1.0);
}

void BgRender::destroyRender() {
releaseTexture();
}

void BgRender::setAnimeConfig(EvaAnimeConfig *config) {
vertexArray->setArray(VertexUtil::create(config->width, config->height, new PointRect(0, 0, config->width, config->height), vertexArray->array));
float* rgba = TexCoordsUtil::create(config->width, config->height, new PointRect(0, 0, config->width, config->height), rgbaArray->array);
rgbaArray->setArray(rgba);
}

GLuint BgRender::getExternalTexture() {
return textureId;
}

void BgRender::releaseTexture() {
glDeleteTextures(1, &textureId);
}

void BgRender::swapBuffers() {

}

void BgRender::updateViewPort(int width, int height) {
if (width <= 0 || height <= 0) return;
surfaceSizeChanged = true;
surfaceWidth = width;
surfaceHeight = height;
}

void BgRender::draw() {
if (textureId != -1) {
glUseProgram(shaderProgram);
vertexArray->setVertexAttribPointer(positionLocation);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
//加载纹理
glUniform1i(uTextureLocation, 0);
rgbaArray->setVertexAttribPointer(textureLocation);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
}
62 changes: 62 additions & 0 deletions yyevac/src/main/cpp/engine/bgrender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Created by zengjiale on 2022/10/11.
//

#include <android/log.h>
#include <engine/irender.h>
#include <egl/eglcore.h>
#include <util/shaderutil.h>
#include <util/glfloatarray.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <util/vertexutil.h>
#include <util/texcoordsutil.h>
#include <util/textureloadutil.h>
#include <android/bitmap.h>

#ifndef YYEVA_BGRENDER_H
#define YYEVA_BGRENDER_H

#define LOG_TAG "BgRender"
#define ELOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define ELOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)


using namespace std;
class BgRender: public IRender {
public:
BgRender();
~BgRender();
void initRender();
void setBgImage(unsigned char *bitmap, AndroidBitmapInfo *bitmapInfo);
void renderFrame();
void clearFrame();
void destroyRender();
void setAnimeConfig(EvaAnimeConfig* config);
GLuint getExternalTexture();
void releaseTexture();
void swapBuffers();
void updateViewPort(int width, int height);
void draw();

private:
GlFloatArray *vertexArray = new GlFloatArray();
GlFloatArray *rgbaArray = new GlFloatArray();

GLuint shaderProgram;
//shader
GLuint textureId;
//顶点位置
GLint uTextureLocation;
//纹理位置
GLint positionLocation;
//纹理位置
GLint textureLocation;

int surfaceWidth = 0;
int surfaceHeight = 0;
bool surfaceSizeChanged = false;
};


#endif //YYEVA_BGRENDER_H
Loading

0 comments on commit 67f321c

Please sign in to comment.