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