不使用video播放mp4

import { _decorator, Component, Node, Sprite, Texture2D, view, SpriteFrame, UITransform } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('WebVideo')

export class WebVideo extends Component {

private cameraNode: Node = null;

private videoStream: MediaStream = null;

private videoTexture: Texture2D = null;

private canvas: HTMLCanvasElement = null;

private context: CanvasRenderingContext2D = null;

private video: HTMLVideoElement = null;

update(deltaTime: number) {

if (this.video && this.videoTexture && this.context) {

try {

this.context.drawImage(this.video, 0, 0, this.canvas.width, this.canvas.height);

this.videoTexture.uploadData(this.canvas);

} catch (error) {

console.error("Error updating videoTexture:", error);

}

}

}

public showCamera() {

this.cameraNode = new Node();

this.node.insertChild(this.cameraNode, 0);

this.startVideoStream();

}

private async startVideoStream() {

try {

this.videoStream = await navigator.mediaDevices.getUserMedia({ video: true });

this.setupVideoElement();

this.setupCanvas();

this.setupVideoTexture();

} catch (err) {

console.error("Error accessing media devices.", err);

}

}

private setupVideoElement() {

this.video = document.createElement('video');

this.video.srcObject = this.videoStream;

this.video.play();

this.video.onloadedmetadata = () => {

this.video.width = this.video.videoWidth;

this.video.height = this.video.videoHeight;

};

}

private setupCanvas() {

this.canvas = document.createElement('canvas');

this.context = this.canvas.getContext('2d');

document.body.appendChild(this.canvas);

this.canvas.style.display = 'none';

const videoTrack = this.videoStream.getVideoTracks()[0];

const videoSettings = videoTrack.getSettings();

this.canvas.width = videoSettings.width;

this.canvas.height = videoSettings.height;

}

private setupVideoTexture() {

this.videoTexture = new Texture2D();

this.videoTexture.reset({

width: this.canvas.width,

height: this.canvas.height,

format: Texture2D.PixelFormat.RGBA8888

});

const sprite = this.cameraNode.addComponent(Sprite);

const spriteFrame = new SpriteFrame();

spriteFrame.texture = this.videoTexture;

sprite.spriteFrame = spriteFrame;

const visibleSize = view.getVisibleSize();

const uiTransform = this.cameraNode.getComponent(UITransform);

uiTransform.setContentSize(visibleSize.width, (this.canvas.height / this.canvas.width) * visibleSize.width);

console.log("UITransform size set to:", visibleSize.width, (this.canvas.height / this.canvas.width) * visibleSize.width);

}

public cleanup() {

if (this.cameraNode) {

this.cameraNode.destroy();

this.cameraNode = null;

}

if (this.videoTexture) {

this.videoTexture.destroy();

this.videoTexture = null;

}

if (this.videoStream) {

this.videoStream.getTracks().forEach(track => track.stop());

this.videoStream = null;

}

if (this.canvas) {

document.body.removeChild(this.canvas);

this.canvas = null;

this.context = null;

}

if (this.video) {

this.video.pause();

this.video.srcObject = null;

this.video = null;

}

}

}

下面我解释一下代码

这段代码实现了一个在Cocos Creator中使用Web视频流的组件`WebVideo`。该组件允许通过摄像头获取视频流并将其显示在Cocos Creator的场景中。下面是对这段代码的详细解释:

导入和装饰器

```typescript

import { _decorator, Component, Node, Sprite, Texture2D, view, SpriteFrame, UITransform } from 'cc';

const { ccclass, property } = _decorator;

```

  • 这部分代码导入了Cocos Creator框架中的一些模块,如`Component`, `Node`, `Sprite`, `Texture2D`, `view`, `SpriteFrame`和`UITransform`。

  • `_decorator`模块用于定义Cocos Creator的类装饰器,如`ccclass`和`property`。

类定义

```typescript

@ccclass('WebVideo')

export class WebVideo extends Component {

private cameraNode: Node = null;

private videoStream: MediaStream = null;

private videoTexture: Texture2D = null;

private canvas: HTMLCanvasElement = null;

private context: CanvasRenderingContext2D = null;

private video: HTMLVideoElement = null;

```

  • `WebVideo`类继承自Cocos Creator的`Component`类。

  • 定义了一些私有变量:`cameraNode`, `videoStream`, `videoTexture`, `canvas`, `context`, 和 `video`,用于管理视频流和显示。

更新方法

```typescript

update(deltaTime: number) {

if (this.video && this.videoTexture && this.context) {

try {

this.context.drawImage(this.video, 0, 0, this.canvas.width, this.canvas.height);

this.videoTexture.uploadData(this.canvas);

} catch (error) {

console.error("Error updating videoTexture:", error);

}

}

}

```

  • `update`方法在每帧调用,用于将视频帧绘制到canvas上,并将canvas的数据上传到`videoTexture`。

  • 通过`drawImage`将视频帧绘制到canvas,再通过`uploadData`上传到纹理。

显示摄像头方法

```typescript

public showCamera() {

this.cameraNode = new Node();

this.node.insertChild(this.cameraNode, 0);

this.startVideoStream();

}

```

  • `showCamera`方法创建一个新的`Node`作为`cameraNode`,并调用`startVideoStream`方法开始视频流。

开始视频流方法

```typescript

private async startVideoStream() {

try {

this.videoStream = await navigator.mediaDevices.getUserMedia({ video: true });

this.setupVideoElement();

this.setupCanvas();

this.setupVideoTexture();

} catch (err) {

console.error("Error accessing media devices.", err);

}

}

```

  • `startVideoStream`方法异步获取视频流,并调用三个方法来设置视频元素、canvas和视频纹理。

设置视频元素

```typescript

private setupVideoElement() {

this.video = document.createElement('video');

this.video.srcObject = this.videoStream;

this.video.play();

this.video.onloadedmetadata = () => {

this.video.width = this.video.videoWidth;

this.video.height = this.video.videoHeight;

};

}

```

  • `setupVideoElement`方法创建一个HTML视频元素,并将视频流设置为视频源。

  • 当视频元数据加载完成时,设置视频的宽度和高度。

设置Canvas

```typescript

private setupCanvas() {

this.canvas = document.createElement('canvas');

this.context = this.canvas.getContext('2d');

document.body.appendChild(this.canvas);

this.canvas.style.display = 'none';

const videoTrack = this.videoStream.getVideoTracks()[0];

const videoSettings = videoTrack.getSettings();

this.canvas.width = videoSettings.width;

this.canvas.height = videoSettings.height;

}

```

  • `setupCanvas`方法创建一个隐藏的canvas元素,用于绘制视频帧。

  • 设置canvas的大小与视频流的大小相同。

设置视频纹理

```typescript

private setupVideoTexture() {

this.videoTexture = new Texture2D();

this.videoTexture.reset({

width: this.canvas.width,

height: this.canvas.height,

format: Texture2D.PixelFormat.RGBA8888

});

const sprite = this.cameraNode.addComponent(Sprite);

const spriteFrame = new SpriteFrame();

spriteFrame.texture = this.videoTexture;

sprite.spriteFrame = spriteFrame;

const visibleSize = view.getVisibleSize();

const uiTransform = this.cameraNode.getComponent(UITransform);

uiTransform.setContentSize(visibleSize.width, (this.canvas.height / this.canvas.width) * visibleSize.width);

console.log("UITransform size set to:", visibleSize.width, (this.canvas.height / this.canvas.width) * visibleSize.width);

}

```

  • `setupVideoTexture`方法创建一个新的`Texture2D`对象并重置它的大小和格式。

  • 创建一个新的`Sprite`组件并将其`spriteFrame`设置为视频纹理。

  • 设置`cameraNode`的大小以匹配屏幕的可见大小,并保持视频的宽高比。

清理方法

```typescript

public cleanup() {

if (this.cameraNode) {

this.cameraNode.destroy();

this.cameraNode = null;

}

if (this.videoTexture) {

this.videoTexture.destroy();

this.videoTexture = null;

}

if (this.videoStream) {

this.videoStream.getTracks().forEach(track => track.stop());

this.videoStream = null;

}

if (this.canvas) {

document.body.removeChild(this.canvas);

this.canvas = null;

this.context = null;

}

if (this.video) {

this.video.pause();

this.video.srcObject = null;

this.video = null;

}

}

}

```

  • `cleanup`方法销毁所有创建的节点、纹理和元素,停止视频流,并清理资源。

欢迎大家来玩我的抖音小游戏

相关推荐
酷讯网络_2408701601 分钟前
【全开源】Java多语言tiktok跨境商城TikTok内嵌商城送搭建教程
java·开发语言·开源
会发光的猪。2 分钟前
vue中el-select选择框带搜索和输入,根据用户输入的值显示下拉列表
前端·javascript·vue.js·elementui
旺旺大力包19 分钟前
【 Git 】git 的安装和使用
前端·笔记·git
PP东24 分钟前
ES学习class类用法(十一)
javascript·学习
海威的技术博客29 分钟前
JS中的原型与原型链
开发语言·javascript·原型模式
雪落满地香35 分钟前
前端:改变鼠标点击物体的颜色
前端
WPG大大通37 分钟前
基于DIODES AP43781+PI3USB31531+PI3DPX1207C的USB-C PD& Video 之全功能显示器连接端口方案
c语言·开发语言·计算机外设·开发板·电源·大大通
从以前1 小时前
【算法题解】Bindian 山丘信号问题(E. Bindian Signaling)
开发语言·python·算法
余生H1 小时前
前端Python应用指南(二)深入Flask:理解Flask的应用结构与模块化设计
前端·后端·python·flask·全栈