@cc-component/cc-ex-component 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,319 @@
1
+ import { _decorator, assetManager, Component, Node, VideoClip, VideoPlayer, view } from 'cc';
2
+ import { BaseVideo, IVideoParam } from './IVideo';
3
+ import { ImageAsset } from 'cc';
4
+ import { Texture2D, SpriteFrame, Sprite, UITransform } from 'cc';
5
+ import { Size } from 'cc';
6
+ import { Vec3 } from 'cc';
7
+ const { ccclass, property } = _decorator;
8
+ declare var tt: any;
9
+ @ccclass('VideoPlayTT')
10
+ export class VideoPlayTT extends BaseVideo {
11
+
12
+ onTimeUpdate: (data: { position: number, duration: number }) => void = null
13
+ pauseFinish: Function
14
+ onError: Function = null
15
+ onVideoEnd: Function = null
16
+ onRead: Function = null
17
+ data: { position: number, duration: number } = {
18
+ position: 0,
19
+ duration: 0
20
+ }
21
+ videoName: string = ""
22
+ videoPlayer: any
23
+ isPlaying: boolean = false
24
+ isReady: boolean = false;
25
+
26
+ getisPause() {
27
+ return !this.isPlaying
28
+ }
29
+ // node: Node;
30
+ taskId: string;
31
+ param: IVideoParam = {
32
+ isLoop: true,
33
+ isLocal: true,
34
+ isPlayTime: false,
35
+ isTargetSize: false
36
+ }
37
+ initParam: { bundle: string, dir: string }
38
+ remoteURL: string;
39
+
40
+ videoTexture: Texture2D;
41
+ imgAsset: ImageAsset;
42
+ video_sp: Sprite
43
+ video_sp_ut: UITransform;
44
+ video_sprite: SpriteFrame;
45
+ canPlay: boolean = false;
46
+ is_stop: boolean = false;
47
+
48
+ initVideo(initParam: { bundle: string, dir: string }) {
49
+ this.initParam = initParam
50
+ this.video_sp = this.node.getComponent(Sprite);
51
+ this.video_sp_ut = this.video_sp.getComponent(UITransform);
52
+ this.imgAsset = new ImageAsset();
53
+ this.videoTexture = new Texture2D();
54
+ this.video_sprite = new SpriteFrame();
55
+
56
+ // const sp = new SpriteFrame()
57
+ // this.videoTexture = new Texture2D();
58
+ // sp.texture = this.videoTexture
59
+ // this.imgAsset = new ImageAsset();
60
+ // this.videoTexture.image = this.imgAsset;
61
+ // this.video_sp.spriteFrame = sp;
62
+ //this.videoPlayer = this.createVideoWX('', true)
63
+ this.actioveSelf(false)
64
+
65
+ // this.video_sp.spriteFrame = new SpriteFrame();
66
+ // this.videoPlayer = this.createVideoWX("", true)
67
+ // this.imgAsset = new ImageAsset();
68
+ // this.imgAsset.reset(this.videoPlayer);
69
+ // this.videoTexture = new Texture2D();
70
+ // this.videoTexture.image = this.imgAsset;
71
+ // this.video_sp.spriteFrame.texture = this.videoTexture;
72
+ // const size = view.getVisibleSize();
73
+ // this.videoPlayer.width = size.width;
74
+ // this.videoPlayer.height = size.height
75
+ //this.video.videoHeight / this.video.videoWidth * width;
76
+ // const uiTransform = this.videoSpriteNode.getComponent(UITransform);
77
+ // uiTransform.width = this.video.width;
78
+ // uiTransform.height = this.video.height;
79
+ //this.video.play();
80
+
81
+
82
+
83
+ }
84
+ //创建视频地址(可能要做缓存处理)
85
+ public createVideoWX(remoteUrl: string, isloop: boolean = false) {
86
+ const self = this;
87
+ const windowInfo = tt.getSystemInfoSync();
88
+ const { windowWidth, windowHeight } = windowInfo;
89
+ let video = tt.createOffscreenVideo();
90
+ video.onError(self.videoError.bind(self))
91
+ video.onEnded(self.videoEnd.bind(self));
92
+ video.onTimeUpdate(self.onUpdate.bind(self))
93
+ video.onPlay(self.onPlay.bind(self))
94
+ video.onPause(self.onPause.bind(self))
95
+ return video;
96
+ }
97
+
98
+ onCanplay() {
99
+ // if (this.is_stop) {
100
+ // console.warn('停止')
101
+ // return
102
+ // } else {
103
+ // }
104
+ console.warn('准备好了')
105
+ this.videoTexture = new Texture2D();
106
+ this.imgAsset.reset(this.videoPlayer);
107
+ this.videoTexture.image = this.imgAsset;
108
+ this.video_sprite.texture = this.videoTexture;
109
+ this.video_sp.spriteFrame = this.video_sprite
110
+
111
+ this.videoPlayer.width = this.videoPlayer.videoWidth;
112
+ this.videoPlayer.height = this.videoPlayer.videoHeight;
113
+ this.canPlay = true;
114
+ this.videoPlayer.play()
115
+ // this.isReady = true;
116
+ // this.readyPlay()
117
+
118
+ }
119
+ public async loadVideo(name: string, param: IVideoParam) {
120
+ this.videoName = name
121
+ this.param = param;
122
+ // 首次调用tt.creatCanvas方法时创建的canvas为上屏canvas,之后创建的均为离屏canvas
123
+
124
+ this.destroyVideo()
125
+ this.isReady = false;
126
+ this.is_stop = false;
127
+ this.videoPlayer = this.createVideoWX(name, true)
128
+ this.actioveSelf(true)
129
+ this.autoSize()
130
+
131
+ this.videoPlayer.src = name;
132
+ this.videoPlayer.onCanplay(this.onCanplay.bind(this))
133
+
134
+ this.videoPlayer.loop = param.isLoop
135
+ const is_skip = name === this.remoteURL;
136
+ this.remoteURL = name;
137
+
138
+ }
139
+
140
+ update(dt: number): void {
141
+ //if (this.videoPlayer && this.video_sp && this.video_sp.spriteFrame && this.isPlaying) {
142
+ if (this.videoPlayer && this.video_sp && this.video_sp.spriteFrame && this.canPlay) {
143
+ this.imgAsset.reset(this.videoPlayer);
144
+ this.videoTexture.updateImage();
145
+ this.video_sp.spriteFrame.texture = this.videoTexture;
146
+ }
147
+ }
148
+
149
+
150
+ // this.video = globalThis.tt.createOffscreenVideo();
151
+ // // 传入视频src
152
+ // this.video.src = "your video url";
153
+ // this.video.onCanplay(() => {
154
+ // const imgAsset = new ImageAsset();
155
+ // imgAsset.reset(this.video);
156
+ // this.videoTexture = new Texture2D();
157
+ // this.videoTexture.image = imgAsset;
158
+ // const spFrame = new SpriteFrame();
159
+ // spFrame.texture = this.videoTexture;
160
+ // this.videoSpriteNode.getComponent(Sprite).spriteFrame = spFrame;
161
+ // const width = view.getVisibleSize().width;
162
+ // this.video.width = width;
163
+ // this.video.height = this.video.videoHeight / this.video.videoWidth * width;
164
+ // const uiTransform = this.videoSpriteNode.getComponent(UITransform);
165
+ // uiTransform.width = this.video.width;
166
+ // uiTransform.height = this.video.height;
167
+ // this.video.play();
168
+ // });
169
+
170
+ play(isloop?: boolean) {
171
+ if (isloop != undefined || isloop != null) {
172
+ this.videoPlayer.loop = isloop
173
+ }
174
+ this.is_stop = false;
175
+
176
+ this.videoPlayer.play();
177
+ this.isPlaying = true
178
+ }
179
+ async pause() {
180
+ super.pause();
181
+ return new Promise<void>((resolve, reject) => {
182
+ this.pauseFinish = resolve;
183
+ this.videoPlayer.pause();
184
+ this.isPlaying = false
185
+
186
+ console.log("暂停")
187
+ })
188
+
189
+ }
190
+ stop() {
191
+ this.videoPlayer.stop();
192
+ this.isPlaying = false
193
+ console.log("停止")
194
+ this.canPlay = false;
195
+
196
+ }
197
+ resume() {
198
+ this.videoPlayer.resume();
199
+ this.isPlaying = true
200
+ }
201
+
202
+ seek(time: number) {
203
+ this.videoPlayer.seek(time)
204
+ }
205
+
206
+ private videoEnd() {
207
+ // if (!this.isloop) this.node.off(VideoPlayer.EventType.COMPLETED, this.videoEnd, this);
208
+ this.onVideoEnd?.();
209
+ }
210
+ private playIng() {
211
+ // if (!this.isloop) this.node.off(VideoPlayer.EventType.PLAYING, this.playIng, this);
212
+ //this.isPlaying = true
213
+ //this.callReadBack && this.callReadBack()
214
+ }
215
+
216
+ private async readyPlay() {
217
+ this.videoPlayer.pause()
218
+ this.onRead?.(this)
219
+
220
+ this.scheduleOnce(() => {
221
+ this.isPlaying = false
222
+ })
223
+ // if (!this.isloop) this.node.off(VideoPlayer.EventType.READY_TO_PLAY, this.readyPlay, this);
224
+ }
225
+ private videoError(err: any) {
226
+
227
+ this.onError?.(err);
228
+ }
229
+ private onPlay() {
230
+ this.isPlaying = true
231
+ if (!this.isReady) {
232
+ this.isReady = true;
233
+ this.is_stop = false
234
+ this.readyPlay()
235
+ }
236
+ this.isReady = true;
237
+ }
238
+ onPause() {
239
+ if (this.pauseFinish) {
240
+ this.pauseFinish()
241
+ this.pauseFinish = null
242
+ }
243
+ }
244
+
245
+ protected onDestroy(): void {
246
+ this.destroyVideo()
247
+ }
248
+
249
+ protected onUpdate(data: any): void {
250
+ if (this.isPlaying && this.param.isPlayTime) {
251
+ this.onTimeUpdate?.(data)
252
+
253
+ }
254
+ }
255
+
256
+ async destroyVideo() {
257
+ if (this.videoPlayer) {
258
+
259
+
260
+ this.is_stop = true
261
+ this.actioveSelf(false)
262
+ this.video_sp.spriteFrame = null
263
+ //this.videoPlayer.x = 2000;
264
+ this.pause()
265
+ console.warn('销毁了视频')
266
+ this.isReady = false;
267
+
268
+ this.videoPlayer.destroy()
269
+ this.videoPlayer.offCanplay(this.onCanplay)
270
+ this.videoPlayer.offError(this.videoError)
271
+ this.videoPlayer.offError(this.videoError)
272
+ this.videoPlayer.offEnded(this.videoEnd);
273
+ this.videoPlayer.offTimeUpdate(this.onUpdate)
274
+ this.videoPlayer.offPlay(this.onPlay)
275
+ this.videoPlayer.offPause(this.onPause)
276
+ this.videoPlayer = null
277
+
278
+ }
279
+ }
280
+
281
+ actioveSelf(show: boolean) {
282
+ this.video_sp.node.active = show
283
+ }
284
+
285
+ setSize(param: { x: number, y: number, width: number, height: number }) {
286
+ if (this.videoPlayer) {
287
+ const size = view.getVisibleSize();
288
+ const designResolutionSize = view.getDesignResolutionSize();
289
+ const ratio = size.width / designResolutionSize.width;
290
+ const width = param.width * ratio;
291
+ const height = param.height * ratio;
292
+ const x = param.x * ratio - width / 2;
293
+ const y = param.y * ratio + param.height / 2 * ratio;
294
+ this.video_sp_ut.node.position = new Vec3(x, y, 0);
295
+ this.video_sp_ut.contentSize = new Size(width, height);
296
+ }
297
+ }
298
+
299
+ // 节点位置适配
300
+ autoSize() {
301
+ if (this.videoPlayer) {
302
+ let size = view.getVisibleSize();
303
+ if (this.param.isFullNot) {
304
+ this.setSize({ x: size.width / 2, y: -size.height / 2, width: 1080, height: 1920 });
305
+ return;
306
+ }
307
+ const designResolutionSize = view.getDesignResolutionSize()
308
+ const ratio = size.height / designResolutionSize.height
309
+
310
+ const width = size.width * ratio
311
+ const x = Math.abs(size.width - width) / 2
312
+ this.video_sp_ut.node.position = new Vec3(-x, 0, 0)
313
+ this.video_sp_ut.contentSize = new Size(width, size.height)
314
+ }
315
+ }
316
+
317
+ }
318
+
319
+
@@ -0,0 +1,9 @@
1
+ {
2
+ "ver": "4.0.24",
3
+ "importer": "typescript",
4
+ "imported": true,
5
+ "uuid": "624a16da-86f2-40dd-b167-a08a6dae88ef",
6
+ "files": [],
7
+ "subMetas": {},
8
+ "userData": {}
9
+ }
@@ -0,0 +1,246 @@
1
+ import { _decorator, assetManager, Component, Node, VideoClip, VideoPlayer, view } from 'cc';
2
+ import { BaseVideo, IVideoParam } from './IVideo';
3
+ const { ccclass, property } = _decorator;
4
+ declare var wx: any;
5
+ @ccclass('VideoPlayWX')
6
+ export class VideoPlayWX extends BaseVideo {
7
+
8
+ onTimeUpdate: (data: { position: number, duration: number }) => void = null
9
+ pauseFinish: Function
10
+ onError: Function = null
11
+ onVideoEnd: Function = null
12
+ onRead: Function = null
13
+ data: { position: number, duration: number } = {
14
+ position: 0,
15
+ duration: 0
16
+ }
17
+ videoName: string = ""
18
+ videoPlayer: any
19
+ isPlaying: boolean = false
20
+ isReady: boolean = false;
21
+
22
+ getisPause() {
23
+ return !this.isPlaying
24
+ }
25
+ // node: Node;
26
+ taskId: string;
27
+ param: IVideoParam = {
28
+ isLoop: true,
29
+ isLocal: true,
30
+ isPlayTime: false,
31
+ isTargetSize: false
32
+ }
33
+ initParam: { bundle: string, dir: string }
34
+ remoteURL: string;
35
+ initVideo(initParam: { bundle: string, dir: string }) {
36
+ this.initParam = initParam
37
+ this.videoPlayer = this.createVideoWX("", true)
38
+
39
+ }
40
+ //创建视频地址(可能要做缓存处理)
41
+ public createVideoWX(remoteUrl: string, isloop: boolean = false) {
42
+ const self = this;
43
+ const windowInfo = wx.getWindowInfo();
44
+ const { windowWidth, windowHeight } = windowInfo;
45
+ let video = wx.createVideo({
46
+ // x: 0,
47
+ // y: 0,
48
+ // width: windowWidth,
49
+ // height: windowHeight,
50
+ //src: remoteUrl,
51
+ // objectFit: "cover",
52
+ // autoplay: true,
53
+ // loop: isLoop,
54
+ // controls: false,
55
+ // underGameView: true,
56
+ // //zIndex: -9999,//好大的坑 官方API根本没写这个
57
+ // showCenterPlayBtn: false,
58
+ // showProgress: false,
59
+ // showProgressInControlMode: false,
60
+ // enableProgressGesture: false,
61
+ x: 0,
62
+ y: 0,
63
+ src: remoteUrl,
64
+ loop: isloop,
65
+ width: windowWidth,
66
+ height: windowHeight,
67
+ controls: false,
68
+ showProgress: false,
69
+ showProgressInControlMode: false,
70
+ autoplay: true,
71
+ showCenterPlayBtn: false,
72
+ underGameView: true,
73
+ enableProgressGesture: false,
74
+ objectFit: "cover",
75
+ //zIndex: -9999,//好大的坑 官方API根本没写这个
76
+ });
77
+ video.onError(self.videoError.bind(self))
78
+ video.onEnded(self.videoEnd.bind(self));
79
+ video.onTimeUpdate(self.onUpdate.bind(self))
80
+ video.onPlay(self.onPlay.bind(self))
81
+ video.onPause(self.onPause.bind(self))
82
+ return video;
83
+ }
84
+
85
+ public async loadVideo(name: string, param: IVideoParam) {
86
+ this.videoName = name
87
+ this.param = param;
88
+ // const is_skip = name === (this.videoPlayer?.src ?? "");
89
+ // if (is_skip) {
90
+ // return;
91
+ // }
92
+ // this.destroyVideo();
93
+ this.isReady = false;
94
+ // this.videoPlayer = this.createVideoWX(name, param.isLoop)
95
+ this.videoPlayer.hide = false;
96
+ this.videoPlayer.x = 0;
97
+ this.videoPlayer.src = name;
98
+ this.videoPlayer.loop = param.isLoop
99
+ const is_skip = name === this.remoteURL;
100
+
101
+ if (is_skip) {
102
+ this.isReady = true;
103
+ this.readyPlay()
104
+ } else {
105
+ this.play()
106
+ }
107
+ this.remoteURL = name;
108
+ }
109
+
110
+ play(isloop?: boolean) {
111
+ if (isloop != undefined || isloop != null) {
112
+ this.videoPlayer.loop = isloop
113
+ }
114
+ this.videoPlayer.play();
115
+ this.isPlaying = true
116
+ }
117
+ async pause() {
118
+ super.pause();
119
+ this.isPlaying = false
120
+ return new Promise<void>((resolve, reject) => {
121
+ this.pauseFinish = resolve;
122
+ this.videoPlayer.pause();
123
+ console.log("暂停")
124
+ })
125
+ }
126
+ stop() {
127
+ this.videoPlayer.stop();
128
+ this.isPlaying = false
129
+ console.log("停止")
130
+
131
+ }
132
+ resume() {
133
+ this.videoPlayer.resume();
134
+ this.isPlaying = true
135
+ }
136
+
137
+ seek(time: number) {
138
+ this.videoPlayer.seek(time)
139
+ }
140
+
141
+ private videoEnd() {
142
+ // if (!this.isloop) this.node.off(VideoPlayer.EventType.COMPLETED, this.videoEnd, this);
143
+ this.onVideoEnd?.();
144
+ }
145
+ private playIng() {
146
+ // if (!this.isloop) this.node.off(VideoPlayer.EventType.PLAYING, this.playIng, this);
147
+ //this.isPlaying = true
148
+ //this.callReadBack && this.callReadBack()
149
+ }
150
+ private async readyPlay() {
151
+ this.videoPlayer.pause();
152
+ // if (!this.isloop) this.node.off(VideoPlayer.EventType.READY_TO_PLAY, this.readyPlay, this);
153
+ this.onRead?.(this)
154
+ }
155
+ private videoError() {
156
+ this.onError?.();
157
+ }
158
+ private onPlay() {
159
+ if (!this.isReady) {
160
+ this.readyPlay()
161
+ }
162
+ this.isReady = true;
163
+ }
164
+ onPause() {
165
+ if (this.pauseFinish) {
166
+ this.pauseFinish()
167
+ this.pauseFinish = null
168
+ }
169
+ }
170
+
171
+ protected onDestroy(): void {
172
+ this.destroyVideo()
173
+ }
174
+
175
+ protected onUpdate(data: any): void {
176
+ if (this.isPlaying && this.param.isPlayTime) {
177
+ this.onTimeUpdate?.(data)
178
+
179
+ }
180
+ }
181
+
182
+ async destroyVideo() {
183
+ if (this.videoPlayer) {
184
+ // this.videoPlayer.offEnded(this.videoEnd)
185
+ // this.videoPlayer.onTimeUpdate(this.onUpdate)
186
+ // this.videoPlayer.offError(this.videoError)
187
+ // this.videoPlayer.offPause(this.onPause)
188
+ // this.videoPlayer.offPlay(this.onPlay)
189
+ //this.videoPlayer.objectFit = "cover"
190
+ this.videoPlayer.hide = true
191
+ this.videoPlayer.x = 2000;
192
+ this.stop()
193
+ this.isReady = false;
194
+ console.error(this.videoPlayer)
195
+ //this.seek(0)
196
+ // this.videoPlayer.src = '';
197
+ //this.videoPlayer.destroy()
198
+ //this.videoPlayer = null
199
+ }
200
+ }
201
+
202
+ setSize(param: { x: number, y: number, width: number, height: number }) {
203
+ if (this.videoPlayer) {
204
+ const windowInfo = wx.getWindowInfo();
205
+ const { windowWidth, windowHeight } = windowInfo;
206
+ const designResolutionSize = view.getDesignResolutionSize()
207
+
208
+ const ratio = windowWidth / designResolutionSize.width
209
+ const width = param.width * ratio
210
+ const height = param.height * ratio
211
+ const x = param.x * ratio - width / 2
212
+ const y = param.y * ratio + param.height / 2 * ratio;
213
+ this.videoPlayer.objectFit = "contain"
214
+ this.videoPlayer.x = x;
215
+ this.videoPlayer.y = y;
216
+ this.videoPlayer.width = width
217
+ this.videoPlayer.height = height
218
+ this.videoPlayer.underGameView = true;
219
+ }
220
+ }
221
+
222
+ // 节点位置适配
223
+ autoSize() {
224
+ if (this.videoPlayer) {
225
+ if (this.param.isFullNot) {
226
+ this.setSize({ x: 0, y: 0, width: 1080, height: 1920 })
227
+ return;
228
+ }
229
+ const designResolutionSize = view.getDesignResolutionSize()
230
+ let windowInfo = wx.getWindowInfo();
231
+ if (this.param.isFullNot) {
232
+ windowInfo = { windowWidth: 1080, windowHeight: 1920 }
233
+ }
234
+ const { windowWidth, windowHeight } = windowInfo;
235
+ this.videoPlayer.objectFit = "cover"
236
+ this.videoPlayer.x = 0;
237
+ this.videoPlayer.y = 0;
238
+ this.videoPlayer.width = windowWidth
239
+ this.videoPlayer.height = windowHeight
240
+ this.videoPlayer.underGameView = true;
241
+ }
242
+ }
243
+
244
+ }
245
+
246
+
@@ -0,0 +1,9 @@
1
+ {
2
+ "ver": "4.0.24",
3
+ "importer": "typescript",
4
+ "imported": true,
5
+ "uuid": "807ce772-7243-4acf-8c65-f94b5a8ee868",
6
+ "files": [],
7
+ "subMetas": {},
8
+ "userData": {}
9
+ }