@eva/plugin-sound 1.2.7-editor.7 → 1.2.7-editor.8

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/dist/miniprogram.js +0 -529
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eva/plugin-sound",
3
- "version": "1.2.7-editor.7",
3
+ "version": "1.2.7-editor.8",
4
4
  "description": "@eva/plugin-sound",
5
5
  "main": "index.js",
6
6
  "module": "dist/plugin-sound.esm.js",
@@ -18,7 +18,7 @@
18
18
  "license": "MIT",
19
19
  "homepage": "https://eva.js.org",
20
20
  "dependencies": {
21
- "@eva/eva.js": "1.2.7-editor.7",
21
+ "@eva/eva.js": "1.2.7-editor.8",
22
22
  "eventemitter3": "^3.1.2"
23
23
  }
24
24
  }
@@ -1,529 +0,0 @@
1
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
- import { windowAlias, documentAlias } from '@eva/miniprogram-adapter';
4
- import { __extends, __values, __awaiter, __generator, __decorate, __metadata } from 'tslib';
5
- import { OBSERVER_TYPE, resource, decorators, System, Component } from '@eva/eva.js/dist/miniprogram';
6
-
7
- var SoundSystem = function (_super) {
8
- __extends(SoundSystem, _super);
9
-
10
- function SoundSystem(obj) {
11
- var _this = _super.call(this) || this;
12
-
13
- _this.autoPauseAndStart = true;
14
- _this.components = [];
15
- _this.pausedComponents = [];
16
- _this.audioBufferCache = {};
17
- _this.decodeAudioPromiseMap = {};
18
-
19
- _extends(_this, obj);
20
-
21
- return _this;
22
- }
23
-
24
- Object.defineProperty(SoundSystem.prototype, "muted", {
25
- get: function get() {
26
- return this.gainNode ? this.gainNode.gain.value === 0 : false;
27
- },
28
- set: function set(v) {
29
- if (!this.gainNode) {
30
- return;
31
- }
32
-
33
- this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
34
- },
35
- enumerable: false,
36
- configurable: true
37
- });
38
- Object.defineProperty(SoundSystem.prototype, "volume", {
39
- get: function get() {
40
- return this.gainNode ? this.gainNode.gain.value : 1;
41
- },
42
- set: function set(v) {
43
- if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
44
- return;
45
- }
46
-
47
- this.gainNode.gain.setValueAtTime(v, 0);
48
- },
49
- enumerable: false,
50
- configurable: true
51
- });
52
- Object.defineProperty(SoundSystem.prototype, "audioLocked", {
53
- get: function get() {
54
- if (!this.ctx) {
55
- return true;
56
- }
57
-
58
- return this.ctx.state !== 'running';
59
- },
60
- enumerable: false,
61
- configurable: true
62
- });
63
-
64
- SoundSystem.prototype.resumeAll = function () {
65
- var _this = this;
66
-
67
- var handleResume = function handleResume() {
68
- _this.pausedComponents.forEach(function (component) {
69
- component.play();
70
- });
71
-
72
- _this.pausedComponents = [];
73
- };
74
-
75
- this.ctx.resume().then(handleResume, handleResume);
76
- };
77
-
78
- SoundSystem.prototype.pauseAll = function () {
79
- var _this = this;
80
-
81
- this.components.forEach(function (component) {
82
- if (component.playing) {
83
- _this.pausedComponents.push(component);
84
-
85
- component.pause();
86
- }
87
- });
88
- this.ctx.suspend().then();
89
- };
90
-
91
- SoundSystem.prototype.stopAll = function () {
92
- this.components.forEach(function (component) {
93
- if (component.playing) {
94
- component.stop();
95
- }
96
- });
97
- this.pausedComponents = [];
98
- this.ctx.suspend().then();
99
- };
100
-
101
- SoundSystem.prototype.init = function () {
102
- this.setupAudioContext();
103
- };
104
-
105
- SoundSystem.prototype.update = function () {
106
- var e_1, _a;
107
-
108
- var changes = this.componentObserver.clear();
109
-
110
- try {
111
- for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
112
- var changed = changes_1_1.value;
113
- this.componentChanged(changed);
114
- }
115
- } catch (e_1_1) {
116
- e_1 = {
117
- error: e_1_1
118
- };
119
- } finally {
120
- try {
121
- if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
122
- } finally {
123
- if (e_1) throw e_1.error;
124
- }
125
- }
126
- };
127
-
128
- SoundSystem.prototype.onResume = function () {
129
- if (!this.autoPauseAndStart) {
130
- return;
131
- }
132
-
133
- this.resumeAll();
134
- };
135
-
136
- SoundSystem.prototype.onPause = function () {
137
- if (!this.autoPauseAndStart) {
138
- return;
139
- }
140
-
141
- this.pauseAll();
142
- };
143
-
144
- SoundSystem.prototype.onDestroy = function () {
145
- this.components.forEach(function (component) {
146
- component.onDestroy();
147
- });
148
- this.components = [];
149
-
150
- if (this.ctx) {
151
- this.gainNode.disconnect();
152
- this.gainNode = null;
153
- this.ctx.close();
154
- this.ctx = null;
155
- }
156
- };
157
-
158
- SoundSystem.prototype.componentChanged = function (changed) {
159
- return __awaiter(this, void 0, void 0, function () {
160
- return __generator(this, function (_a) {
161
- if (changed.componentName !== 'Sound') return [2];
162
-
163
- if (changed.type === OBSERVER_TYPE.ADD) {
164
- this.add(changed);
165
- }
166
-
167
- return [2];
168
- });
169
- });
170
- };
171
-
172
- SoundSystem.prototype.setupAudioContext = function () {
173
- try {
174
- var AudioContext_1 = windowAlias.AudioContext || windowAlias.webkitAudioContext;
175
- this.ctx = new AudioContext_1();
176
- } catch (error) {
177
- console.error(error);
178
-
179
- if (this.onError) {
180
- this.onError(error);
181
- }
182
- }
183
-
184
- if (!this.ctx) {
185
- return;
186
- }
187
-
188
- this.gainNode = typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
189
- this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
190
- this.gainNode.connect(this.ctx.destination);
191
- this.unlockAudio();
192
- };
193
-
194
- SoundSystem.prototype.unlockAudio = function () {
195
- var _this = this;
196
-
197
- if (!this.ctx || !this.audioLocked) {
198
- return;
199
- }
200
-
201
- var unlock = function unlock() {
202
- if (_this.ctx) {
203
- var removeListenerFn = function removeListenerFn() {
204
- documentAlias.body.removeEventListener('touchstart', unlock);
205
- documentAlias.body.removeEventListener('touchend', unlock);
206
- documentAlias.body.removeEventListener('click', unlock);
207
- };
208
-
209
- _this.ctx.resume().then(removeListenerFn, removeListenerFn);
210
- }
211
- };
212
-
213
- documentAlias.body.addEventListener('touchstart', unlock);
214
- documentAlias.body.addEventListener('touchend', unlock);
215
- documentAlias.body.addEventListener('click', unlock);
216
- };
217
-
218
- SoundSystem.prototype.add = function (changed) {
219
- var _a;
220
-
221
- return __awaiter(this, void 0, void 0, function () {
222
- var component, config, audio, _b, _c, error_1;
223
-
224
- return __generator(this, function (_d) {
225
- switch (_d.label) {
226
- case 0:
227
- component = changed.component;
228
- this.components.push(component);
229
- _d.label = 1;
230
-
231
- case 1:
232
- _d.trys.push([1, 5,, 6]);
233
-
234
- config = component.config;
235
- component.state = 'loading';
236
- return [4, resource.getResource(config.resource)];
237
-
238
- case 2:
239
- audio = _d.sent();
240
- if (!(!this.audioBufferCache[audio.name] && ((_a = audio === null || audio === void 0 ? void 0 : audio.data) === null || _a === void 0 ? void 0 : _a.audio))) return [3, 4];
241
- _b = this.audioBufferCache;
242
- _c = audio.name;
243
- return [4, this.decodeAudioData(audio.data.audio, audio.name)];
244
-
245
- case 3:
246
- _b[_c] = _d.sent();
247
- _d.label = 4;
248
-
249
- case 4:
250
- if (this.audioBufferCache[audio.name]) {
251
- component.systemContext = this.ctx;
252
- component.systemDestination = this.gainNode;
253
- component.onload(this.audioBufferCache[audio.name]);
254
- }
255
-
256
- return [3, 6];
257
-
258
- case 5:
259
- error_1 = _d.sent();
260
-
261
- if (this.onError) {
262
- this.onError(error_1);
263
- }
264
-
265
- return [3, 6];
266
-
267
- case 6:
268
- return [2];
269
- }
270
- });
271
- });
272
- };
273
-
274
- SoundSystem.prototype.decodeAudioData = function (arraybuffer, name) {
275
- var _this = this;
276
-
277
- if (this.decodeAudioPromiseMap[name]) {
278
- return this.decodeAudioPromiseMap[name];
279
- }
280
-
281
- var promise = new Promise(function (resolve, reject) {
282
- if (!_this.ctx) {
283
- reject(new Error('No audio support'));
284
- }
285
-
286
- var success = function success(decodedData) {
287
- if (_this.decodeAudioPromiseMap[name]) {
288
- delete _this.decodeAudioPromiseMap[name];
289
- }
290
-
291
- if (decodedData) {
292
- resolve(decodedData);
293
- } else {
294
- reject(new Error("Error decoding audio " + name));
295
- }
296
- };
297
-
298
- var error = function error(err) {
299
- if (_this.decodeAudioPromiseMap[name]) {
300
- delete _this.decodeAudioPromiseMap[name];
301
- }
302
-
303
- reject(new Error(err + ". arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
304
- };
305
-
306
- var promise = _this.ctx.decodeAudioData(arraybuffer, success, error);
307
-
308
- if (promise instanceof Promise) {
309
- promise.catch(function (err) {
310
- reject(new Error("catch " + err + ", arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
311
- });
312
- }
313
- });
314
- this.decodeAudioPromiseMap[name] = promise;
315
- return promise;
316
- };
317
-
318
- SoundSystem.systemName = 'SoundSystem';
319
- SoundSystem = __decorate([decorators.componentObserver({
320
- Sound: []
321
- }), __metadata("design:paramtypes", [Object])], SoundSystem);
322
- return SoundSystem;
323
- }(System);
324
-
325
- var SoundSystem$1 = SoundSystem;
326
-
327
- var Sound = function (_super) {
328
- __extends(Sound, _super);
329
-
330
- function Sound() {
331
- var _this = _super !== null && _super.apply(this, arguments) || this;
332
-
333
- _this.state = 'unloaded';
334
- _this.config = {
335
- resource: '',
336
- autoplay: false,
337
- muted: false,
338
- volume: 1,
339
- loop: false,
340
- seek: 0
341
- };
342
- _this.playTime = 0;
343
- _this.startTime = 0;
344
- _this.duration = 0;
345
- _this.actionQueue = [];
346
- return _this;
347
- }
348
-
349
- Object.defineProperty(Sound.prototype, "muted", {
350
- get: function get() {
351
- return this.gainNode ? this.gainNode.gain.value === 0 : false;
352
- },
353
- set: function set(v) {
354
- if (!this.gainNode) {
355
- return;
356
- }
357
-
358
- this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
359
- },
360
- enumerable: false,
361
- configurable: true
362
- });
363
- Object.defineProperty(Sound.prototype, "volume", {
364
- get: function get() {
365
- return this.gainNode ? this.gainNode.gain.value : 1;
366
- },
367
- set: function set(v) {
368
- if (typeof v !== 'number' || v < 0 || v > 1) {
369
- return;
370
- }
371
-
372
- this.config.volume = v;
373
-
374
- if (!this.gainNode) {
375
- return;
376
- }
377
-
378
- this.gainNode.gain.setValueAtTime(v, 0);
379
- },
380
- enumerable: false,
381
- configurable: true
382
- });
383
-
384
- Sound.prototype.init = function (obj) {
385
- if (!obj) {
386
- return;
387
- }
388
-
389
- _extends(this.config, obj);
390
-
391
- if (this.config.autoplay) {
392
- this.actionQueue.push(this.play.bind(this));
393
- }
394
- };
395
-
396
- Sound.prototype.play = function () {
397
- var _this = this;
398
-
399
- if (this.state !== 'loaded') {
400
- this.actionQueue.push(this.play.bind(this));
401
- }
402
-
403
- this.destroySource();
404
- this.createSource();
405
-
406
- if (!this.sourceNode) {
407
- return;
408
- }
409
-
410
- var when = this.systemContext.currentTime;
411
- var offset = this.config.seek;
412
- var duration = this.config.duration;
413
- this.sourceNode.start(0, offset, duration);
414
- this.startTime = when;
415
- this.playTime = when - offset;
416
- this.paused = false;
417
- this.playing = true;
418
- this.resetConfig();
419
-
420
- this.endedListener = function () {
421
- if (!_this.sourceNode) {
422
- return;
423
- }
424
-
425
- if (_this.config.onEnd) {
426
- _this.config.onEnd();
427
- }
428
-
429
- if (_this.playing) {
430
- _this.destroySource();
431
- }
432
- };
433
-
434
- this.sourceNode.addEventListener('ended', this.endedListener);
435
- };
436
-
437
- Sound.prototype.pause = function () {
438
- if (this.state !== 'loaded') {
439
- this.actionQueue.push(this.pause.bind(this));
440
- }
441
-
442
- if (this.paused || !this.playing) {
443
- return;
444
- }
445
-
446
- this.paused = true;
447
- this.playing = false;
448
- this.config.seek = this.getCurrentTime();
449
- this.destroySource();
450
- };
451
-
452
- Sound.prototype.stop = function () {
453
- if (this.state !== 'loaded') {
454
- this.actionQueue.push(this.stop.bind(this));
455
- }
456
-
457
- if (!this.paused && !this.playing) {
458
- return;
459
- }
460
-
461
- this.playing = false;
462
- this.paused = false;
463
- this.destroySource();
464
- this.resetConfig();
465
- };
466
-
467
- Sound.prototype.onload = function (buffer) {
468
- this.state = 'loaded';
469
- this.buffer = buffer;
470
- this.duration = this.buffer.duration;
471
- this.actionQueue.forEach(function (action) {
472
- return action();
473
- });
474
- this.actionQueue.length = 0;
475
- };
476
-
477
- Sound.prototype.onDestroy = function () {
478
- this.actionQueue.length = 0;
479
- this.destroySource();
480
- };
481
-
482
- Sound.prototype.resetConfig = function () {
483
- this.config.seek = 0;
484
- };
485
-
486
- Sound.prototype.getCurrentTime = function () {
487
- if (this.config.loop && this.duration > 0) {
488
- return (this.systemContext.currentTime - this.playTime) % this.duration;
489
- }
490
-
491
- return this.systemContext.currentTime - this.playTime;
492
- };
493
-
494
- Sound.prototype.createSource = function () {
495
- if (!this.systemContext || this.state !== 'loaded') {
496
- return;
497
- }
498
-
499
- this.sourceNode = this.systemContext.createBufferSource();
500
- this.sourceNode.buffer = this.buffer;
501
- this.sourceNode.loop = this.config.loop;
502
-
503
- if (!this.gainNode) {
504
- this.gainNode = this.systemContext.createGain();
505
- this.gainNode.connect(this.systemDestination);
506
-
507
- _extends(this, this.config);
508
- }
509
-
510
- this.sourceNode.connect(this.gainNode);
511
- };
512
-
513
- Sound.prototype.destroySource = function () {
514
- if (!this.sourceNode) return;
515
- this.sourceNode.removeEventListener('ended', this.endedListener);
516
- this.sourceNode.stop();
517
- this.sourceNode.disconnect();
518
- this.sourceNode = null;
519
- this.startTime = 0;
520
- this.playTime = 0;
521
- this.playing = false;
522
- };
523
-
524
- Sound.componentName = 'Sound';
525
- return Sound;
526
- }(Component);
527
-
528
- var Sound$1 = Sound;
529
- export { Sound$1 as Sound, SoundSystem$1 as SoundSystem };