[LayaAir 2.0]Laya 使用VideoTexture播放视频

最近因需要在Laya上播放视频,看了下帖子,都说是要使用
Laya.Browser.createElement("video")
创建html元素来播放视频,这样做和Laya引擎的结合太差,可定制性也很大,关键时有时候需要对视频界面做一些动画效果时就很麻烦,
于是就看看其他引擎的做法。首先看到Unity上的MovieTexture,然后想到了使用纹理贴图来播放;然后就搜索到three.js上有一个通过html的VideoElement来实现MovieTexture的项目。查看源码,发现是使用了gl.texImage2D这个webgl接口,百度一下之后:
void gl.texImage2D(target, level, internalformat, width, height, border, format, type, HTMLVideoElement source);

最后一个参数支持HTMLVideoElement对象,于是就想看看Laya又没有封装这个接口,全局搜索之后果然有这个接口:loadImageSource
于是就尝试了一下,果然是可以的,但是Laya好像没有提供更新Texture的接口,只能自己手动调用定时器来更新。下方有Demo,欢迎评论和指正~
已邀请:

hfxxxxxxhf

赞同来自:

大神,这个在微信的播放器上默认全屏,然后给了video标签一些属性后不会全屏播放,但是面片上也不会播放了。用手机浏览器是正常的

181*****858

赞同来自:

大佬新版本浏览器就跑步起来了GL_INVALID_OPERATION: Internal D3D11 error: HRESULT: 0x80070057: Error allocating ShaderResourceView

181*****858

赞同来自:

GL_INVALID_OPERATION: Internal D3D11 error: HRESULT: 0x80070057: Error allocating ShaderResourceView 大佬新版本chrome 跑步起来版本 98.0.4758.9(正式版本)dev (64 位)

181*****858

赞同来自:

GL_INVALID_OPERATION: Internal D3D11 error: HRESULT: 0x80070057: Error allocating ShaderResourceView 大佬新版本chrome 跑步起来版本 98.0.4758.9(正式版本)dev (64 位)

Laya_XS

赞同来自:

目前LayaAir2.0中完整的解决方案代码可以参考下:
/**
* example:videodom 使用Laya.HtmlVideo播放视频
* @ author:xs
* @ data: 2024-03-05 16:20
*/
export default class VideoDom {
htmlvideo:Laya.HtmlVideo;
constructor() {
Laya.stage.on(Laya.Event.CLICK,this,this.onClick);
}

onClick(){
this.initVideo();
}
initVideo() {
this.htmlvideo = new Laya.HtmlVideo();
this.htmlvideo.setSource("https://www.layaair.com/3.x/de ... ot%3B,1);
this.htmlvideo.video.addEventListener("loadedmetadata",()=>{
var videoTexture:Laya.VideoTexture = new Laya.VideoTexture();
videoTexture.video = this.htmlvideo.video;
videoTexture.video.play();
videoTexture.video.loop = true;
var bitmap = new Laya.Texture2D(videoTexture.video.videoWidth,videoTexture.video.videoHeight);
var texture:Laya.Texture = new Laya.Texture(bitmap);

var image:Laya.Image = new Laya.Image();
image.texture = texture;
image.width = videoTexture.video.videoWidth;
image.height = videoTexture.video.videoHeight;
image.pos(100,100);
Laya.stage.addChild(image);
//目前引擎暂时没有提供刷新机制,需要手动刷新。或者你可以试用LayaAir3.0引擎,那边实现videoTexture不用自己手动刷新
Laya.timer.frameLoop(1, this, this.updateVideo, [bitmap]);

},true);
}

updateVideo(tempTexture:Laya.Texture2D){
if(this.htmlvideo.video.readyState === this.htmlvideo.video.HAVE_ENOUGH_DATA) {
//更新视频纹理
tempTexture.loadImageSource(this.htmlvideo.video);
}
}
}

要回复问题请先

商务合作
商务合作