@galacean/effects-plugin-multimedia 2.1.0-alpha.7

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.
package/dist/index.js ADDED
@@ -0,0 +1,930 @@
1
+ /*!
2
+ * Name: @galacean/effects-plugin-multimedia
3
+ * Description: Galacean Effects player multimedia plugin
4
+ * Author: Ant Group CO., Ltd.
5
+ * Contributors: 云垣
6
+ * Version: v2.1.0-alpha.7
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ Object.defineProperty(exports, '__esModule', { value: true });
12
+
13
+ var effects = require('@galacean/effects');
14
+
15
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
16
+ try {
17
+ var info = gen[key](arg);
18
+ var value = info.value;
19
+ } catch (error) {
20
+ reject(error);
21
+ return;
22
+ }
23
+ if (info.done) resolve(value);
24
+ else Promise.resolve(value).then(_next, _throw);
25
+ }
26
+ function _async_to_generator(fn) {
27
+ return function() {
28
+ var self = this, args = arguments;
29
+ return new Promise(function(resolve, reject) {
30
+ var gen = fn.apply(self, args);
31
+ function _next(value) {
32
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
33
+ }
34
+ function _throw(err) {
35
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
36
+ }
37
+ _next(undefined);
38
+ });
39
+ };
40
+ }
41
+
42
+ function _set_prototype_of(o, p) {
43
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
44
+ o.__proto__ = p;
45
+ return o;
46
+ };
47
+ return _set_prototype_of(o, p);
48
+ }
49
+
50
+ function _inherits(subClass, superClass) {
51
+ if (typeof superClass !== "function" && superClass !== null) {
52
+ throw new TypeError("Super expression must either be null or a function");
53
+ }
54
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
55
+ constructor: {
56
+ value: subClass,
57
+ writable: true,
58
+ configurable: true
59
+ }
60
+ });
61
+ if (superClass) _set_prototype_of(subClass, superClass);
62
+ }
63
+
64
+ function _instanceof1(left, right) {
65
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
66
+ return !!right[Symbol.hasInstance](left);
67
+ } else return _instanceof1(left, right);
68
+ }
69
+
70
+ function __decorate(decorators, target, key, desc) {
71
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
72
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
73
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
74
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
75
+ }
76
+ function __generator(thisArg, body) {
77
+ var _ = {
78
+ label: 0,
79
+ sent: function sent() {
80
+ if (t[0] & 1) throw t[1];
81
+ return t[1];
82
+ },
83
+ trys: [],
84
+ ops: []
85
+ }, f, y, t, g;
86
+ return g = {
87
+ next: verb(0),
88
+ "throw": verb(1),
89
+ "return": verb(2)
90
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
91
+ return this;
92
+ }), g;
93
+ function verb(n) {
94
+ return function(v) {
95
+ return step([
96
+ n,
97
+ v
98
+ ]);
99
+ };
100
+ }
101
+ function step(op) {
102
+ if (f) throw new TypeError("Generator is already executing.");
103
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
104
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
105
+ if (y = 0, t) op = [
106
+ op[0] & 2,
107
+ t.value
108
+ ];
109
+ switch(op[0]){
110
+ case 0:
111
+ case 1:
112
+ t = op;
113
+ break;
114
+ case 4:
115
+ _.label++;
116
+ return {
117
+ value: op[1],
118
+ done: false
119
+ };
120
+ case 5:
121
+ _.label++;
122
+ y = op[1];
123
+ op = [
124
+ 0
125
+ ];
126
+ continue;
127
+ case 7:
128
+ op = _.ops.pop();
129
+ _.trys.pop();
130
+ continue;
131
+ default:
132
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
133
+ _ = 0;
134
+ continue;
135
+ }
136
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
137
+ _.label = op[1];
138
+ break;
139
+ }
140
+ if (op[0] === 6 && _.label < t[1]) {
141
+ _.label = t[1];
142
+ t = op;
143
+ break;
144
+ }
145
+ if (t && _.label < t[2]) {
146
+ _.label = t[2];
147
+ _.ops.push(op);
148
+ break;
149
+ }
150
+ if (t[2]) _.ops.pop();
151
+ _.trys.pop();
152
+ continue;
153
+ }
154
+ op = body.call(thisArg, _);
155
+ } catch (e) {
156
+ op = [
157
+ 6,
158
+ e
159
+ ];
160
+ y = 0;
161
+ } finally{
162
+ f = t = 0;
163
+ }
164
+ if (op[0] & 5) throw op[1];
165
+ return {
166
+ value: op[0] ? op[1] : void 0,
167
+ done: true
168
+ };
169
+ }
170
+ }
171
+ typeof SuppressedError === "function" ? SuppressedError : function _SuppressedError(error, suppressed, message) {
172
+ var e = new Error(message);
173
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
174
+ };
175
+
176
+ function _assert_this_initialized(self) {
177
+ if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
178
+ return self;
179
+ }
180
+
181
+ function _is_native_reflect_construct() {
182
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
183
+ if (Reflect.construct.sham) return false;
184
+ if (typeof Proxy === "function") return true;
185
+ try {
186
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
187
+ return true;
188
+ } catch (e) {
189
+ return false;
190
+ }
191
+ }
192
+
193
+ function _construct(Parent, args, Class) {
194
+ if (_is_native_reflect_construct()) _construct = Reflect.construct;
195
+ else {
196
+ _construct = function construct(Parent, args, Class) {
197
+ var a = [
198
+ null
199
+ ];
200
+ a.push.apply(a, args);
201
+ var Constructor = Function.bind.apply(Parent, a);
202
+ var instance = new Constructor();
203
+ if (Class) _set_prototype_of(instance, Class.prototype);
204
+ return instance;
205
+ };
206
+ }
207
+ return _construct.apply(null, arguments);
208
+ }
209
+
210
+ function _get_prototype_of(o) {
211
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
212
+ return o.__proto__ || Object.getPrototypeOf(o);
213
+ };
214
+ return _get_prototype_of(o);
215
+ }
216
+
217
+ function _is_native_function(fn) {
218
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
219
+ }
220
+
221
+ function _wrap_native_super(Class) {
222
+ var _cache = typeof Map === "function" ? new Map() : undefined;
223
+ _wrap_native_super = function _wrap_native_super(Class) {
224
+ if (Class === null || !_is_native_function(Class)) return Class;
225
+ if (typeof Class !== "function") throw new TypeError("Super expression must either be null or a function");
226
+ if (typeof _cache !== "undefined") {
227
+ if (_cache.has(Class)) return _cache.get(Class);
228
+ _cache.set(Class, Wrapper);
229
+ }
230
+ function Wrapper() {
231
+ return _construct(Class, arguments, _get_prototype_of(this).constructor);
232
+ }
233
+ Wrapper.prototype = Object.create(Class.prototype, {
234
+ constructor: {
235
+ value: Wrapper,
236
+ enumerable: false,
237
+ writable: true,
238
+ configurable: true
239
+ }
240
+ });
241
+ return _set_prototype_of(Wrapper, Class);
242
+ };
243
+ return _wrap_native_super(Class);
244
+ }
245
+
246
+ var multimediaErrorMessageMap = {
247
+ 2000: "Autoplay permission for audio and video is not enabled"
248
+ };
249
+ var multimediaErrorDisplayMessageMap = {
250
+ 2000: "音视频自动播放权限未开启"
251
+ };
252
+
253
+ function processMultimedia(media, type, options) {
254
+ return _processMultimedia.apply(this, arguments);
255
+ }
256
+ function _processMultimedia() {
257
+ _processMultimedia = _async_to_generator(function(media, type, options) {
258
+ var renderLevel, jobs;
259
+ return __generator(this, function(_state) {
260
+ renderLevel = options.renderLevel;
261
+ jobs = media.map(function(medium) {
262
+ if (effects.passRenderLevel(medium.renderLevel, renderLevel)) {
263
+ var url = new URL(medium.url, location.href).href;
264
+ if (type === effects.spec.MultimediaType.video) {
265
+ return effects.loadVideo(url);
266
+ } else if (type === effects.spec.MultimediaType.audio) {
267
+ return loadAudio(url);
268
+ }
269
+ }
270
+ throw new Error("Invalid " + type + " source: " + JSON.stringify(media) + ".");
271
+ });
272
+ return [
273
+ 2,
274
+ Promise.all(jobs)
275
+ ];
276
+ });
277
+ });
278
+ return _processMultimedia.apply(this, arguments);
279
+ }
280
+ /**
281
+ * 判断音视频浏览器是否允许播放
282
+ */ function checkAutoplayPermission() {
283
+ return _checkAutoplayPermission.apply(this, arguments);
284
+ }
285
+ function _checkAutoplayPermission() {
286
+ _checkAutoplayPermission = _async_to_generator(function() {
287
+ var audio;
288
+ return __generator(this, function(_state) {
289
+ switch(_state.label){
290
+ case 0:
291
+ audio = document.createElement("audio");
292
+ // src 为长度为 0.1s 的音频片段
293
+ audio.src = "data:audio/mpeg;base64,//uQxAACFCEW8uewc8Nfu50FhJuQAGAFJCAhaIHoMhfEnBzixrwNsCGGGmyeCECGGRKWwnB0MkzGh6Hn7JLEstwCADQsJwBAAAOOhOAGAcd0gJgTBuW7HBgJBgfvEgCAEBEiOyeTDxyiyjZLEsRDyEzMz+921nJyWJZn6w8P1769e/AYCQI6tIJAkGHL16zpxhY5VeYGCdd3/93d9w4tygIO/4IHBOD8Hz/4f+IDkEHU4n8PqBMMBCQ0iXWYFnCIGqooaZHfRqOrgxuOtjmpCJCaYjmQqDz3NJUBTFWK4soYEoCumIJzIBldNhLUmAaDzhggZmSAkr9SqAIjGJFGEMCQlIPKDMuo24qZIrKDONHWGqlZbOymMy2yhCoBQywFQAgBEsETW0hCoIkqQWBINPWa3rCoEg1MiBIEiZMUiklyMfKVqoUOxIkSMtVTMzOMSBiKJQMAiWyROrf/5mq//8mkknNBQlFjiJFHKqqr//1VV6qq3vNVVbJpFHXkijM+pIoy1VX7zPOJEkmJAJaYgpqKBgASEAAIAAAAAAAAAAAA//uSxAAD1p0iZiw9OIgAADSAAAAEYAwFApgokRqIFjigukAADJhFjIUGoGFlRAycQGC5QsJJRWdRZRVWZVNYBStcjsRuaiMSgmOQOvVAKBSBY4ygsJGDCEoUEWrE3NBfUyJSSTTiPMpDUUvrhTsC7XR/G6bx2nse52G+cjBDhQW5tan7IZREJY6ULlDpCIhCIhkPDYgFaN2//3JZVZVY6UXQO2NXV1u//P/KSqyqSai7GFdu0DEqoheCVpA1v///6qqaaaYRKqIGKpiCmooGABIQAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==";
294
+ audio.muted = true;
295
+ audio.crossOrigin = "anonymous";
296
+ _state.label = 1;
297
+ case 1:
298
+ _state.trys.push([
299
+ 1,
300
+ 3,
301
+ ,
302
+ 4
303
+ ]);
304
+ return [
305
+ 4,
306
+ audio.play()
307
+ ];
308
+ case 2:
309
+ _state.sent();
310
+ return [
311
+ 3,
312
+ 4
313
+ ];
314
+ case 3:
315
+ _state.sent();
316
+ throw new MultimediaError(2000, multimediaErrorMessageMap[2000]);
317
+ case 4:
318
+ return [
319
+ 2
320
+ ];
321
+ }
322
+ });
323
+ });
324
+ return _checkAutoplayPermission.apply(this, arguments);
325
+ }
326
+ /**
327
+ * 异步加载一个音频文件
328
+ * @param url - 音频文件的 URL 或 MediaProvider 对象
329
+ */ function loadAudio(url) {
330
+ return _loadAudio.apply(this, arguments);
331
+ }
332
+ function _loadAudio() {
333
+ _loadAudio = _async_to_generator(function(url) {
334
+ var isSupportAudioContext, audioContext, buffer, decodedData;
335
+ return __generator(this, function(_state) {
336
+ switch(_state.label){
337
+ case 0:
338
+ isSupportAudioContext = !!window["AudioContext"];
339
+ if (!isSupportAudioContext) return [
340
+ 3,
341
+ 5
342
+ ];
343
+ _state.label = 1;
344
+ case 1:
345
+ _state.trys.push([
346
+ 1,
347
+ 4,
348
+ ,
349
+ 5
350
+ ]);
351
+ audioContext = new AudioContext();
352
+ return [
353
+ 4,
354
+ effects.loadBinary(url)
355
+ ];
356
+ case 2:
357
+ buffer = _state.sent();
358
+ return [
359
+ 4,
360
+ audioContext.decodeAudioData(buffer)
361
+ ];
362
+ case 3:
363
+ decodedData = _state.sent();
364
+ return [
365
+ 2,
366
+ decodedData
367
+ ];
368
+ case 4:
369
+ _state.sent();
370
+ throw new Error("Failed to load audio from " + url + ".");
371
+ case 5:
372
+ return [
373
+ 2,
374
+ new Promise(function(resolve, reject) {
375
+ var audio = new Audio();
376
+ // 设置音频源
377
+ if (typeof url === "string") {
378
+ audio.src = url;
379
+ } else {
380
+ audio.srcObject = url;
381
+ }
382
+ audio.muted = false;
383
+ // 监听加载事件
384
+ audio.addEventListener("canplaythrough", function() {
385
+ resolve(audio);
386
+ });
387
+ // 监听错误事件
388
+ audio.addEventListener("error", function() {
389
+ reject(new Error("Failed to load audio from " + audio.src + "."));
390
+ });
391
+ // 开始加载音频
392
+ audio.load();
393
+ })
394
+ ];
395
+ }
396
+ });
397
+ });
398
+ return _loadAudio.apply(this, arguments);
399
+ }
400
+ var MultimediaError = /*#__PURE__*/ function(Error1) {
401
+ _inherits(MultimediaError, Error1);
402
+ function MultimediaError(code, message) {
403
+ var _this;
404
+ _this = Error1.call(this, message) || this;
405
+ _this.code = code;
406
+ _this.name = _this.constructor.name;
407
+ if ("captureStackTrace" in Error && effects.isFunction(Error.captureStackTrace)) {
408
+ Error.captureStackTrace(_assert_this_initialized(_this), MultimediaError);
409
+ }
410
+ return _this;
411
+ }
412
+ return MultimediaError;
413
+ }(_wrap_native_super(Error));
414
+
415
+ var VideoLoader = /*#__PURE__*/ function(AbstractPlugin) {
416
+ _inherits(VideoLoader, AbstractPlugin);
417
+ function VideoLoader() {
418
+ return AbstractPlugin.apply(this, arguments);
419
+ }
420
+ VideoLoader.processAssets = function processAssets(json, options) {
421
+ if (options === void 0) options = {};
422
+ return _async_to_generator(function() {
423
+ var _json_videos, videos, loadedAssets;
424
+ return __generator(this, function(_state) {
425
+ switch(_state.label){
426
+ case 0:
427
+ _json_videos = json.videos, videos = _json_videos === void 0 ? [] : _json_videos;
428
+ return [
429
+ 4,
430
+ processMultimedia(videos, effects.spec.MultimediaType.video, options)
431
+ ];
432
+ case 1:
433
+ loadedAssets = _state.sent();
434
+ return [
435
+ 2,
436
+ {
437
+ assets: videos,
438
+ loadedAssets: loadedAssets
439
+ }
440
+ ];
441
+ }
442
+ });
443
+ })();
444
+ };
445
+ return VideoLoader;
446
+ }(effects.AbstractPlugin);
447
+
448
+ var AudioLoader = /*#__PURE__*/ function(AbstractPlugin) {
449
+ _inherits(AudioLoader, AbstractPlugin);
450
+ function AudioLoader() {
451
+ return AbstractPlugin.apply(this, arguments);
452
+ }
453
+ AudioLoader.processAssets = function processAssets(json, options) {
454
+ if (options === void 0) options = {};
455
+ return _async_to_generator(function() {
456
+ var _json_audios, audios, loadedAssets;
457
+ return __generator(this, function(_state) {
458
+ switch(_state.label){
459
+ case 0:
460
+ _json_audios = json.audios, audios = _json_audios === void 0 ? [] : _json_audios;
461
+ return [
462
+ 4,
463
+ processMultimedia(audios, effects.spec.MultimediaType.audio, options)
464
+ ];
465
+ case 1:
466
+ loadedAssets = _state.sent();
467
+ return [
468
+ 2,
469
+ {
470
+ assets: audios,
471
+ loadedAssets: loadedAssets
472
+ }
473
+ ];
474
+ }
475
+ });
476
+ })();
477
+ };
478
+ return AudioLoader;
479
+ }(effects.AbstractPlugin);
480
+
481
+ var seed = 0;
482
+ exports.VideoComponent = /*#__PURE__*/ function(BaseRenderComponent) {
483
+ _inherits(VideoComponent, BaseRenderComponent);
484
+ function VideoComponent(engine) {
485
+ var _this;
486
+ _this = BaseRenderComponent.call(this, engine) || this;
487
+ _this.threshold = 0.03;
488
+ _this.name = "MVideo" + seed++;
489
+ _this.geometry = _this.createGeometry(effects.glContext.TRIANGLES);
490
+ return _this;
491
+ }
492
+ var _proto = VideoComponent.prototype;
493
+ _proto.setTexture = function setTexture(texture) {
494
+ var oldTexture = this.renderer.texture;
495
+ var composition = this.item.composition;
496
+ if (!composition) {
497
+ return;
498
+ }
499
+ composition.textures.forEach(function(cachedTexture, index) {
500
+ if (cachedTexture === oldTexture) {
501
+ composition.textures[index] = texture;
502
+ }
503
+ });
504
+ this.engine.removeTexture(oldTexture);
505
+ this.renderer.texture = texture;
506
+ this.material.setTexture("uSampler0", texture);
507
+ this.video = texture.source.video;
508
+ };
509
+ _proto.fromData = function fromData(data) {
510
+ BaseRenderComponent.prototype.fromData.call(this, data);
511
+ var interaction = data.interaction, options = data.options, _data_listIndex = data.listIndex, listIndex = _data_listIndex === void 0 ? 0 : _data_listIndex;
512
+ var video = options.video, _options_startColor = options.startColor, startColor = _options_startColor === void 0 ? [
513
+ 1,
514
+ 1,
515
+ 1,
516
+ 1
517
+ ] : _options_startColor, _options_playbackRate = options.playbackRate, playbackRate = _options_playbackRate === void 0 ? 1 : _options_playbackRate, _options_volume = options.volume, volume = _options_volume === void 0 ? 1 : _options_volume, _options_muted = options.muted, muted = _options_muted === void 0 ? false : _options_muted;
518
+ var renderer = data.renderer;
519
+ if (!renderer) {
520
+ renderer = {};
521
+ }
522
+ if (video) {
523
+ this.video = video.data;
524
+ this.setPlaybackRate(playbackRate);
525
+ this.setVolume(volume);
526
+ this.setMuted(muted);
527
+ var endBehavior = this.item.endBehavior;
528
+ // 如果元素设置为 destroy
529
+ if (endBehavior === effects.spec.EndBehavior.destroy) {
530
+ this.setLoop(false);
531
+ }
532
+ }
533
+ var _renderer_renderMode, _renderer_blending, _renderer_texture, _renderer_side, _renderer_mask, _renderer_maskMode;
534
+ this.renderer = {
535
+ renderMode: (_renderer_renderMode = renderer.renderMode) != null ? _renderer_renderMode : effects.spec.RenderMode.BILLBOARD,
536
+ blending: (_renderer_blending = renderer.blending) != null ? _renderer_blending : effects.spec.BlendingMode.ALPHA,
537
+ texture: (_renderer_texture = renderer.texture) != null ? _renderer_texture : this.engine.emptyTexture,
538
+ occlusion: !!renderer.occlusion,
539
+ transparentOcclusion: !!renderer.transparentOcclusion || renderer.maskMode === effects.spec.MaskMode.MASK,
540
+ side: (_renderer_side = renderer.side) != null ? _renderer_side : effects.spec.SideMode.DOUBLE,
541
+ mask: (_renderer_mask = renderer.mask) != null ? _renderer_mask : 0,
542
+ maskMode: (_renderer_maskMode = renderer.maskMode) != null ? _renderer_maskMode : effects.spec.MaskMode.NONE,
543
+ order: listIndex
544
+ };
545
+ this.interaction = interaction;
546
+ this.pauseVideo();
547
+ this.setItem();
548
+ this.material.setVector4("_Color", new effects.math.Vector4().setFromArray(startColor));
549
+ };
550
+ _proto.onUpdate = function onUpdate(dt) {
551
+ var _this_item_composition;
552
+ BaseRenderComponent.prototype.onUpdate.call(this, dt);
553
+ var _this_item = this.item, time = _this_item.time, duration = _this_item.duration, endBehavior = _this_item.endBehavior;
554
+ if (time > 0) {
555
+ this.setVisible(true);
556
+ this.playVideo();
557
+ }
558
+ if (time === 0 && ((_this_item_composition = this.item.composition) == null ? void 0 : _this_item_composition.rootItem.endBehavior) === effects.spec.EndBehavior.freeze) {
559
+ this.pauseVideo();
560
+ this.setCurrentTime(0);
561
+ }
562
+ if (Math.abs(time - duration) < this.threshold) {
563
+ if (endBehavior === effects.spec.EndBehavior.freeze) {
564
+ this.pauseVideo();
565
+ } else if (endBehavior === effects.spec.EndBehavior.restart) {
566
+ this.setVisible(false);
567
+ // 重播
568
+ this.pauseVideo();
569
+ this.setCurrentTime(0);
570
+ }
571
+ }
572
+ };
573
+ /**
574
+ * 获取当前视频时长
575
+ * @returns 视频时长
576
+ */ _proto.getDuration = function getDuration() {
577
+ return this.video ? this.video.duration : 0;
578
+ };
579
+ /**
580
+ * 获取当前视频播放时刻
581
+ * @returns 当前视频播放时刻
582
+ */ _proto.getCurrentTime = function getCurrentTime() {
583
+ return this.video ? this.video.currentTime : 0;
584
+ };
585
+ /**
586
+ * 设置阈值(由于视频是单独的 update,有时并不能完全对其 GE 的 update)
587
+ * @param threshold 阈值
588
+ */ _proto.setThreshold = function setThreshold(threshold) {
589
+ this.threshold = threshold;
590
+ };
591
+ /**
592
+ * 设置当前视频播放时刻
593
+ * @param time 视频播放时刻
594
+ */ _proto.setCurrentTime = function setCurrentTime(time) {
595
+ if (this.video) {
596
+ this.video.currentTime = time;
597
+ }
598
+ };
599
+ /**
600
+ * 设置视频是否循环播放
601
+ * @param loop 是否循环播放
602
+ */ _proto.setLoop = function setLoop(loop) {
603
+ if (this.video) {
604
+ this.video.loop = loop;
605
+ }
606
+ };
607
+ /**
608
+ * 设置视频是否静音
609
+ * @param muted 是否静音
610
+ */ _proto.setMuted = function setMuted(muted) {
611
+ if (this.video && this.video.muted !== muted) {
612
+ this.video.muted = muted;
613
+ }
614
+ };
615
+ /**
616
+ * 设置视频音量
617
+ * @param volume 视频音量
618
+ */ _proto.setVolume = function setVolume(volume) {
619
+ if (this.video && this.video.volume !== volume) {
620
+ this.video.volume = volume;
621
+ }
622
+ };
623
+ /**
624
+ * 设置视频播放速率
625
+ * @param rate 视频播放速率
626
+ */ _proto.setPlaybackRate = function setPlaybackRate(rate) {
627
+ if (!this.video || this.video.playbackRate === rate) {
628
+ return;
629
+ }
630
+ this.video.playbackRate = rate;
631
+ };
632
+ _proto.playVideo = function playVideo() {
633
+ var _this = this;
634
+ if (this.video) {
635
+ this.video.play().catch(function(error) {
636
+ _this.engine.renderErrors.add(error);
637
+ });
638
+ }
639
+ };
640
+ _proto.pauseVideo = function pauseVideo() {
641
+ if (this.video && !this.video.paused) {
642
+ this.video.pause();
643
+ }
644
+ };
645
+ _proto.onDisable = function onDisable() {
646
+ var _this_video;
647
+ BaseRenderComponent.prototype.onDisable.call(this);
648
+ this.setCurrentTime(0);
649
+ (_this_video = this.video) == null ? void 0 : _this_video.pause();
650
+ };
651
+ _proto.onEnable = function onEnable() {
652
+ BaseRenderComponent.prototype.onEnable.call(this);
653
+ this.playVideo();
654
+ };
655
+ return VideoComponent;
656
+ }(effects.BaseRenderComponent);
657
+ exports.VideoComponent = __decorate([
658
+ effects.effectsClass(effects.spec.DataType.VideoComponent)
659
+ ], exports.VideoComponent);
660
+
661
+ var AudioPlayer = /*#__PURE__*/ function() {
662
+ function AudioPlayer(audio, engine) {
663
+ this.engine = engine;
664
+ this.audioSourceInfo = {};
665
+ this.isSupportAudioContext = !!window["AudioContext"];
666
+ this.options = {
667
+ endBehavior: effects.spec.EndBehavior.destroy,
668
+ duration: 0
669
+ };
670
+ this.destroyed = false;
671
+ this.started = false;
672
+ this.initialized = false;
673
+ this.currentVolume = 1;
674
+ this.setAudioSource(audio);
675
+ }
676
+ var _proto = AudioPlayer.prototype;
677
+ /**
678
+ * 设置音频资源
679
+ * @param audio - 音频资源
680
+ */ _proto.setAudioSource = function setAudioSource(audio) {
681
+ if (this.audio || this.audioSourceInfo.source) {
682
+ this.dispose();
683
+ }
684
+ if (_instanceof1(audio, AudioBuffer)) {
685
+ var audioContext = new AudioContext();
686
+ var gainNode = audioContext.createGain();
687
+ gainNode.connect(audioContext.destination);
688
+ var source = audioContext.createBufferSource();
689
+ source.buffer = audio;
690
+ source.connect(gainNode);
691
+ this.audioSourceInfo = {
692
+ source: source,
693
+ audioContext: audioContext,
694
+ gainNode: gainNode
695
+ };
696
+ } else {
697
+ this.audio = audio;
698
+ }
699
+ if (this.started) {
700
+ this.play();
701
+ }
702
+ };
703
+ _proto.getCurrentTime = function getCurrentTime() {
704
+ if (this.isSupportAudioContext) {
705
+ var audioContext = this.audioSourceInfo.audioContext;
706
+ return (audioContext == null ? void 0 : audioContext.currentTime) || 0;
707
+ } else {
708
+ var _this_audio;
709
+ return ((_this_audio = this.audio) == null ? void 0 : _this_audio.currentTime) || 0;
710
+ }
711
+ };
712
+ _proto.play = function play() {
713
+ var _this = this;
714
+ if (this.isSupportAudioContext) {
715
+ var _this_audioSourceInfo = this.audioSourceInfo, audioContext = _this_audioSourceInfo.audioContext, source = _this_audioSourceInfo.source;
716
+ if (source && audioContext) {
717
+ switch(this.options.endBehavior){
718
+ case effects.spec.EndBehavior.destroy:
719
+ case effects.spec.EndBehavior.freeze:
720
+ source.start(0);
721
+ break;
722
+ case effects.spec.EndBehavior.restart:
723
+ source.loop = true;
724
+ source.loopStart = 0;
725
+ source.loopEnd = this.options.duration;
726
+ source.start(0);
727
+ break;
728
+ }
729
+ }
730
+ this.started = true;
731
+ } else {
732
+ var _this_audio;
733
+ (_this_audio = this.audio) == null ? void 0 : _this_audio.play().catch(function(e) {
734
+ _this.engine.renderErrors.add(e);
735
+ });
736
+ }
737
+ };
738
+ _proto.pause = function pause() {
739
+ if (this.isSupportAudioContext) {
740
+ var _this_audioSourceInfo = this.audioSourceInfo, source = _this_audioSourceInfo.source, audioContext = _this_audioSourceInfo.audioContext;
741
+ if (!audioContext) {
742
+ return;
743
+ }
744
+ if (audioContext.currentTime > 0 && this.started) {
745
+ source == null ? void 0 : source.stop();
746
+ }
747
+ } else {
748
+ var _this_audio;
749
+ (_this_audio = this.audio) == null ? void 0 : _this_audio.pause();
750
+ }
751
+ };
752
+ _proto.setVolume = function setVolume(volume) {
753
+ if (this.isSupportAudioContext) {
754
+ var gainNode = this.audioSourceInfo.gainNode;
755
+ if (gainNode) {
756
+ gainNode.gain.value = volume;
757
+ this.currentVolume = volume;
758
+ }
759
+ } else {
760
+ if (this.audio) {
761
+ this.audio.volume = volume;
762
+ this.currentVolume = volume;
763
+ }
764
+ }
765
+ };
766
+ _proto.setPlaybackRate = function setPlaybackRate(rate) {
767
+ if (this.isSupportAudioContext) {
768
+ var source = this.audioSourceInfo.source;
769
+ if (source) {
770
+ source.playbackRate.value = rate;
771
+ }
772
+ } else {
773
+ if (this.audio) {
774
+ this.audio.playbackRate = rate;
775
+ }
776
+ }
777
+ };
778
+ _proto.setLoop = function setLoop(loop) {
779
+ if (this.isSupportAudioContext) {
780
+ var source = this.audioSourceInfo.source;
781
+ if (!source) {
782
+ this.engine.renderErrors.add(new Error("Audio source is not found."));
783
+ } else {
784
+ source.loop = loop;
785
+ }
786
+ } else {
787
+ if (this.audio) {
788
+ this.audio.loop = loop;
789
+ }
790
+ }
791
+ };
792
+ _proto.setOptions = function setOptions(options) {
793
+ if (this.initialized) {
794
+ return;
795
+ }
796
+ this.options = options;
797
+ this.initialized = true;
798
+ };
799
+ _proto.setMuted = function setMuted(muted) {
800
+ if (this.isSupportAudioContext) {
801
+ var gainNode = this.audioSourceInfo.gainNode;
802
+ var value = muted ? 0 : this.currentVolume;
803
+ if (gainNode) {
804
+ gainNode.gain.value = value;
805
+ }
806
+ } else {
807
+ if (this.audio) {
808
+ this.audio.muted = muted;
809
+ }
810
+ }
811
+ };
812
+ _proto.dispose = function dispose() {
813
+ var _this = this;
814
+ if (this.destroyed) {
815
+ return;
816
+ }
817
+ if (this.isSupportAudioContext) {
818
+ var _this_audioSourceInfo = this.audioSourceInfo, audioContext = _this_audioSourceInfo.audioContext, source = _this_audioSourceInfo.source;
819
+ if (this.started) {
820
+ source == null ? void 0 : source.stop();
821
+ }
822
+ audioContext == null ? void 0 : audioContext.close().catch(function(e) {
823
+ _this.engine.renderErrors.add(e);
824
+ });
825
+ } else {
826
+ var _this_audio;
827
+ (_this_audio = this.audio) == null ? void 0 : _this_audio.pause();
828
+ }
829
+ this.destroyed = true;
830
+ };
831
+ return AudioPlayer;
832
+ }();
833
+
834
+ exports.AudioComponent = /*#__PURE__*/ function(RendererComponent) {
835
+ _inherits(AudioComponent, RendererComponent);
836
+ function AudioComponent() {
837
+ var _this;
838
+ _this = RendererComponent.apply(this, arguments) || this;
839
+ _this.isVideoPlay = false;
840
+ _this.threshold = 0.03;
841
+ return _this;
842
+ }
843
+ var _proto = AudioComponent.prototype;
844
+ _proto.onUpdate = function onUpdate(dt) {
845
+ RendererComponent.prototype.onUpdate.call(this, dt);
846
+ var _this_item = this.item, time = _this_item.time, duration = _this_item.duration, endBehavior = _this_item.endBehavior;
847
+ this.audioPlayer.setOptions({
848
+ duration: duration,
849
+ endBehavior: endBehavior
850
+ });
851
+ if (time >= 0 && !this.isVideoPlay) {
852
+ this.audioPlayer.play();
853
+ this.isVideoPlay = true;
854
+ }
855
+ if (Math.abs(time - duration) < this.threshold) {
856
+ if (endBehavior === effects.spec.EndBehavior.destroy) {
857
+ this.audioPlayer.pause();
858
+ }
859
+ }
860
+ };
861
+ _proto.fromData = function fromData(data) {
862
+ RendererComponent.prototype.fromData.call(this, data);
863
+ var options = data.options;
864
+ var _options_playbackRate = options.playbackRate, playbackRate = _options_playbackRate === void 0 ? 1 : _options_playbackRate, _options_muted = options.muted, muted = _options_muted === void 0 ? false : _options_muted, _options_volume = options.volume, volume = _options_volume === void 0 ? 1 : _options_volume;
865
+ this.audioPlayer = new AudioPlayer(options.audio.data, this.engine);
866
+ this.audioPlayer.pause();
867
+ this.setPlaybackRate(playbackRate);
868
+ this.setMuted(muted);
869
+ this.setVolume(volume);
870
+ };
871
+ /**
872
+ * 设置音频资源
873
+ * @param audio - 音频资源
874
+ */ _proto.setAudioSource = function setAudioSource(audio) {
875
+ this.audioPlayer.setAudioSource(audio);
876
+ };
877
+ /**
878
+ * 设置音量
879
+ * @param volume - 音量
880
+ */ _proto.setVolume = function setVolume(volume) {
881
+ this.audioPlayer.setVolume(volume);
882
+ };
883
+ /**
884
+ * 获取当前音频的播放时刻
885
+ */ _proto.getCurrentTime = function getCurrentTime() {
886
+ return this.audioPlayer.getCurrentTime();
887
+ };
888
+ /**
889
+ * 设置是否静音
890
+ * @param muted - 是否静音
891
+ */ _proto.setMuted = function setMuted(muted) {
892
+ this.audioPlayer.setMuted(muted);
893
+ };
894
+ /**
895
+ * 设置是否循环播放
896
+ * @param loop - 是否循环播放
897
+ */ _proto.setLoop = function setLoop(loop) {
898
+ this.audioPlayer.setLoop(loop);
899
+ };
900
+ /**
901
+ * 设置播放速率
902
+ * @param rate - 播放速率
903
+ */ _proto.setPlaybackRate = function setPlaybackRate(rate) {
904
+ this.audioPlayer.setPlaybackRate(rate);
905
+ };
906
+ _proto.onDisable = function onDisable() {
907
+ RendererComponent.prototype.onDisable.call(this);
908
+ this.audioPlayer.pause();
909
+ };
910
+ _proto.dispose = function dispose() {
911
+ RendererComponent.prototype.dispose.call(this);
912
+ this.audioPlayer.dispose();
913
+ };
914
+ return AudioComponent;
915
+ }(effects.RendererComponent);
916
+ exports.AudioComponent = __decorate([
917
+ effects.effectsClass(effects.spec.DataType.AudioComponent)
918
+ ], exports.AudioComponent);
919
+
920
+ effects.registerPlugin("video", VideoLoader, effects.VFXItem, true);
921
+ effects.registerPlugin("audio", AudioLoader, effects.VFXItem, true);
922
+
923
+ exports.AudioPlayer = AudioPlayer;
924
+ exports.MultimediaError = MultimediaError;
925
+ exports.checkAutoplayPermission = checkAutoplayPermission;
926
+ exports.loadAudio = loadAudio;
927
+ exports.multimediaErrorDisplayMessageMap = multimediaErrorDisplayMessageMap;
928
+ exports.multimediaErrorMessageMap = multimediaErrorMessageMap;
929
+ exports.processMultimedia = processMultimedia;
930
+ //# sourceMappingURL=index.js.map