@jswork/howler-audio-manager 0.0.0-stage → 1.0.2
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/LICENSE.txt +21 -0
- package/README.md +257 -2
- package/dist/index.cjs.js +747 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.mts +87 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.esm.js +546 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +648 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +66 -4
- package/src/index.ts +5 -0
- package/src/manager.ts +317 -0
- package/src/react.tsx +98 -0
- package/src/types.ts +40 -0
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
function _array_like_to_array(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _array_with_holes(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
9
|
+
function _class_call_check(instance, Constructor) {
|
|
10
|
+
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
|
|
11
|
+
}
|
|
12
|
+
function _defineProperties(target, props) {
|
|
13
|
+
for(var i = 0; i < props.length; i++){
|
|
14
|
+
var descriptor = props[i];
|
|
15
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
16
|
+
descriptor.configurable = true;
|
|
17
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
18
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
22
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
23
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
24
|
+
return Constructor;
|
|
25
|
+
}
|
|
26
|
+
function _iterable_to_array_limit(arr, i) {
|
|
27
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
28
|
+
if (_i == null) return;
|
|
29
|
+
var _arr = [];
|
|
30
|
+
var _n = true;
|
|
31
|
+
var _d = false;
|
|
32
|
+
var _s, _e;
|
|
33
|
+
try {
|
|
34
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
35
|
+
_arr.push(_s.value);
|
|
36
|
+
if (i && _arr.length === i) break;
|
|
37
|
+
}
|
|
38
|
+
} catch (err) {
|
|
39
|
+
_d = true;
|
|
40
|
+
_e = err;
|
|
41
|
+
} finally{
|
|
42
|
+
try {
|
|
43
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
44
|
+
} finally{
|
|
45
|
+
if (_d) throw _e;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return _arr;
|
|
49
|
+
}
|
|
50
|
+
function _non_iterable_rest() {
|
|
51
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
52
|
+
}
|
|
53
|
+
function _sliced_to_array(arr, i) {
|
|
54
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
55
|
+
}
|
|
56
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
57
|
+
if (!o) return;
|
|
58
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
59
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
60
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
61
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
62
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
63
|
+
}
|
|
64
|
+
/*!
|
|
65
|
+
* name: @jswork/howler-audio-manager
|
|
66
|
+
* description: Howler.js audio manager, enum-driven sounds for H5.
|
|
67
|
+
* homepage: https://js.work
|
|
68
|
+
* version: 1.0.2
|
|
69
|
+
* date: 2026-10-20 20:03:06
|
|
70
|
+
* license: MIT
|
|
71
|
+
*/ // src/manager.ts
|
|
72
|
+
import { Howl, Howler } from "howler";
|
|
73
|
+
var AudioManager = /*#__PURE__*/ function() {
|
|
74
|
+
"use strict";
|
|
75
|
+
function AudioManager(options, inject) {
|
|
76
|
+
_class_call_check(this, AudioManager);
|
|
77
|
+
var _ref, _ref1;
|
|
78
|
+
this.effectCache = /* @__PURE__ */ new Map();
|
|
79
|
+
this.urlAudioCache = /* @__PURE__ */ new Map();
|
|
80
|
+
this.preloadPromises = /* @__PURE__ */ new Map();
|
|
81
|
+
this.activeUrlHowl = null;
|
|
82
|
+
this.globalVolume = 1;
|
|
83
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
84
|
+
this.HowlConstructor = (_ref = inject === null || inject === void 0 ? void 0 : inject.HowlClass) !== null && _ref !== void 0 ? _ref : Howl;
|
|
85
|
+
this.HowlerInstance = (_ref1 = inject === null || inject === void 0 ? void 0 : inject.HowlerObj) !== null && _ref1 !== void 0 ? _ref1 : Howler;
|
|
86
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.defaultVolume) === "number") {
|
|
87
|
+
this.setGlobalVolume(options.defaultVolume);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
_create_class(AudioManager, [
|
|
91
|
+
{
|
|
92
|
+
key: "hasEffect",
|
|
93
|
+
value: function hasEffect(key) {
|
|
94
|
+
return this.effectCache.has(key);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
key: "loadEffect",
|
|
99
|
+
value: function loadEffect(key, url) {
|
|
100
|
+
var _this = this;
|
|
101
|
+
if (this.effectCache.has(key)) {
|
|
102
|
+
this.effectCache.get(key).unload();
|
|
103
|
+
}
|
|
104
|
+
var howl = new this.HowlConstructor({
|
|
105
|
+
src: [
|
|
106
|
+
url
|
|
107
|
+
],
|
|
108
|
+
preload: true
|
|
109
|
+
});
|
|
110
|
+
howl.on("load", function() {
|
|
111
|
+
_this.emit("load", {
|
|
112
|
+
type: "effect",
|
|
113
|
+
key: key,
|
|
114
|
+
url: url
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
howl.on("loaderror", function(_id, error) {
|
|
118
|
+
_this.emit("error", {
|
|
119
|
+
type: "effect",
|
|
120
|
+
key: key,
|
|
121
|
+
url: url,
|
|
122
|
+
error: error
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
this.effectCache.set(key, howl);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
key: "loadEffects",
|
|
130
|
+
value: function loadEffects(map) {
|
|
131
|
+
var _this = this;
|
|
132
|
+
Object.keys(map).forEach(function(key) {
|
|
133
|
+
var typedKey = isNaN(Number(key)) ? key : Number(key);
|
|
134
|
+
_this.loadEffect(typedKey, map[key]);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
key: "playEffect",
|
|
140
|
+
value: function playEffect(key, options) {
|
|
141
|
+
var _this = this;
|
|
142
|
+
var howl = this.effectCache.get(key);
|
|
143
|
+
if (!howl) {
|
|
144
|
+
console.warn("[AudioManager] Effect not found for key: ".concat(String(key), "."));
|
|
145
|
+
return void 0;
|
|
146
|
+
}
|
|
147
|
+
if (howl.playing()) {
|
|
148
|
+
howl.stop();
|
|
149
|
+
}
|
|
150
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
|
|
151
|
+
howl.volume(options.volume);
|
|
152
|
+
}
|
|
153
|
+
howl.off("play");
|
|
154
|
+
howl.off("end");
|
|
155
|
+
howl.off("stop");
|
|
156
|
+
howl.off("playerror");
|
|
157
|
+
howl.once("play", function(soundId) {
|
|
158
|
+
var _options_onStart;
|
|
159
|
+
options === null || options === void 0 ? void 0 : (_options_onStart = options.onStart) === null || _options_onStart === void 0 ? void 0 : _options_onStart.call(options, soundId);
|
|
160
|
+
_this.emit("play", {
|
|
161
|
+
type: "effect",
|
|
162
|
+
key: key,
|
|
163
|
+
soundId: soundId
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
howl.once("end", function(soundId) {
|
|
167
|
+
var _options_onEnd;
|
|
168
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
|
|
169
|
+
howl.volume(1);
|
|
170
|
+
}
|
|
171
|
+
options === null || options === void 0 ? void 0 : (_options_onEnd = options.onEnd) === null || _options_onEnd === void 0 ? void 0 : _options_onEnd.call(options, soundId);
|
|
172
|
+
_this.emit("end", {
|
|
173
|
+
type: "effect",
|
|
174
|
+
key: key,
|
|
175
|
+
soundId: soundId
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
howl.once("stop", function(soundId) {
|
|
179
|
+
var _options_onStop;
|
|
180
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
|
|
181
|
+
howl.volume(1);
|
|
182
|
+
}
|
|
183
|
+
options === null || options === void 0 ? void 0 : (_options_onStop = options.onStop) === null || _options_onStop === void 0 ? void 0 : _options_onStop.call(options, soundId);
|
|
184
|
+
_this.emit("stop", {
|
|
185
|
+
type: "effect",
|
|
186
|
+
key: key,
|
|
187
|
+
soundId: soundId
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
howl.once("playerror", function(soundId, error) {
|
|
191
|
+
var _options_onError;
|
|
192
|
+
options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, soundId, error);
|
|
193
|
+
_this.emit("error", {
|
|
194
|
+
type: "effect",
|
|
195
|
+
key: key,
|
|
196
|
+
soundId: soundId,
|
|
197
|
+
error: error
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
return howl.play();
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
key: "stopEffect",
|
|
205
|
+
value: function stopEffect(key) {
|
|
206
|
+
var howl = this.effectCache.get(key);
|
|
207
|
+
if (howl) {
|
|
208
|
+
howl.stop();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
key: "removeEffect",
|
|
214
|
+
value: function removeEffect(key) {
|
|
215
|
+
var howl = this.effectCache.get(key);
|
|
216
|
+
if (howl) {
|
|
217
|
+
howl.unload();
|
|
218
|
+
this.effectCache.delete(key);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
key: "preloadUrl",
|
|
224
|
+
value: function preloadUrl(url) {
|
|
225
|
+
var _this = this;
|
|
226
|
+
var existing = this.urlAudioCache.get(url);
|
|
227
|
+
if (existing && existing.state() === "loaded") {
|
|
228
|
+
return Promise.resolve();
|
|
229
|
+
}
|
|
230
|
+
var pending = this.preloadPromises.get(url);
|
|
231
|
+
if (pending) {
|
|
232
|
+
return pending;
|
|
233
|
+
}
|
|
234
|
+
var howl = existing && existing.state() !== "unloaded" ? existing : this.createUrlHowl(url);
|
|
235
|
+
var promise = new Promise(function(resolve, reject) {
|
|
236
|
+
howl.once("load", function() {
|
|
237
|
+
resolve();
|
|
238
|
+
});
|
|
239
|
+
howl.once("loaderror", function(_id, error) {
|
|
240
|
+
_this.urlAudioCache.delete(url);
|
|
241
|
+
reject(error);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
this.preloadPromises.set(url, promise);
|
|
245
|
+
var settle = function settle() {
|
|
246
|
+
if (_this.preloadPromises.get(url) === promise) {
|
|
247
|
+
_this.preloadPromises.delete(url);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
promise.then(settle, settle);
|
|
251
|
+
return promise;
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
key: "preloadUrls",
|
|
256
|
+
value: function preloadUrls(urls) {
|
|
257
|
+
var _this = this;
|
|
258
|
+
return Promise.all(urls.map(function(url) {
|
|
259
|
+
return _this.preloadUrl(url);
|
|
260
|
+
}));
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
key: "createUrlHowl",
|
|
265
|
+
value: function createUrlHowl(url) {
|
|
266
|
+
var _this = this;
|
|
267
|
+
var howl = new this.HowlConstructor({
|
|
268
|
+
src: [
|
|
269
|
+
url
|
|
270
|
+
],
|
|
271
|
+
preload: true
|
|
272
|
+
});
|
|
273
|
+
howl.on("load", function() {
|
|
274
|
+
_this.emit("load", {
|
|
275
|
+
type: "url",
|
|
276
|
+
url: url
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
howl.on("loaderror", function(_id, error) {
|
|
280
|
+
_this.urlAudioCache.delete(url);
|
|
281
|
+
_this.emit("error", {
|
|
282
|
+
type: "url",
|
|
283
|
+
url: url,
|
|
284
|
+
error: error
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
this.urlAudioCache.set(url, howl);
|
|
288
|
+
return howl;
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
key: "playUrl",
|
|
293
|
+
value: function playUrl(url, options) {
|
|
294
|
+
var _this = this;
|
|
295
|
+
var howl = this.urlAudioCache.get(url) || this.createUrlHowl(url);
|
|
296
|
+
var isSingleton = (options === null || options === void 0 ? void 0 : options.singleton) !== false;
|
|
297
|
+
if (isSingleton && this.activeUrlHowl && this.activeUrlHowl !== howl && this.activeUrlHowl.playing()) {
|
|
298
|
+
this.activeUrlHowl.stop();
|
|
299
|
+
}
|
|
300
|
+
if (howl.playing()) {
|
|
301
|
+
howl.stop();
|
|
302
|
+
}
|
|
303
|
+
if ((options === null || options === void 0 ? void 0 : options.loop) !== void 0) {
|
|
304
|
+
howl.loop(options.loop);
|
|
305
|
+
}
|
|
306
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
|
|
307
|
+
howl.volume(options.volume);
|
|
308
|
+
}
|
|
309
|
+
if (isSingleton) {
|
|
310
|
+
this.activeUrlHowl = howl;
|
|
311
|
+
}
|
|
312
|
+
howl.off("play");
|
|
313
|
+
howl.off("end");
|
|
314
|
+
howl.off("stop");
|
|
315
|
+
howl.off("playerror");
|
|
316
|
+
howl.once("play", function(soundId) {
|
|
317
|
+
var _options_onStart;
|
|
318
|
+
options === null || options === void 0 ? void 0 : (_options_onStart = options.onStart) === null || _options_onStart === void 0 ? void 0 : _options_onStart.call(options, soundId);
|
|
319
|
+
_this.emit("play", {
|
|
320
|
+
type: "url",
|
|
321
|
+
url: url,
|
|
322
|
+
soundId: soundId
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
howl.once("end", function(soundId) {
|
|
326
|
+
var _options_onEnd;
|
|
327
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
|
|
328
|
+
howl.volume(1);
|
|
329
|
+
}
|
|
330
|
+
options === null || options === void 0 ? void 0 : (_options_onEnd = options.onEnd) === null || _options_onEnd === void 0 ? void 0 : _options_onEnd.call(options, soundId);
|
|
331
|
+
_this.emit("end", {
|
|
332
|
+
type: "url",
|
|
333
|
+
url: url,
|
|
334
|
+
soundId: soundId
|
|
335
|
+
});
|
|
336
|
+
if (_this.activeUrlHowl === howl && !howl.loop()) {
|
|
337
|
+
_this.activeUrlHowl = null;
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
howl.once("stop", function(soundId) {
|
|
341
|
+
var _options_onStop;
|
|
342
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.volume) === "number") {
|
|
343
|
+
howl.volume(1);
|
|
344
|
+
}
|
|
345
|
+
options === null || options === void 0 ? void 0 : (_options_onStop = options.onStop) === null || _options_onStop === void 0 ? void 0 : _options_onStop.call(options, soundId);
|
|
346
|
+
_this.emit("stop", {
|
|
347
|
+
type: "url",
|
|
348
|
+
url: url,
|
|
349
|
+
soundId: soundId
|
|
350
|
+
});
|
|
351
|
+
if (_this.activeUrlHowl === howl) {
|
|
352
|
+
_this.activeUrlHowl = null;
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
howl.once("playerror", function(soundId, error) {
|
|
356
|
+
var _options_onError;
|
|
357
|
+
options === null || options === void 0 ? void 0 : (_options_onError = options.onError) === null || _options_onError === void 0 ? void 0 : _options_onError.call(options, soundId, error);
|
|
358
|
+
_this.emit("error", {
|
|
359
|
+
type: "url",
|
|
360
|
+
url: url,
|
|
361
|
+
soundId: soundId,
|
|
362
|
+
error: error
|
|
363
|
+
});
|
|
364
|
+
if (_this.activeUrlHowl === howl) {
|
|
365
|
+
_this.activeUrlHowl = null;
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
return howl.play();
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
key: "stopUrl",
|
|
373
|
+
value: function stopUrl(url) {
|
|
374
|
+
var howl = this.urlAudioCache.get(url);
|
|
375
|
+
if (howl) {
|
|
376
|
+
howl.stop();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
key: "on",
|
|
382
|
+
value: function on(event, handler) {
|
|
383
|
+
var _this_listeners_get;
|
|
384
|
+
var set = (_this_listeners_get = this.listeners.get(event)) !== null && _this_listeners_get !== void 0 ? _this_listeners_get : /* @__PURE__ */ new Set();
|
|
385
|
+
this.listeners.set(event, set);
|
|
386
|
+
set.add(handler);
|
|
387
|
+
return {
|
|
388
|
+
destroy: function destroy() {
|
|
389
|
+
set.delete(handler);
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
key: "emit",
|
|
396
|
+
value: function emit(event, payload) {
|
|
397
|
+
var set = this.listeners.get(event);
|
|
398
|
+
if (set) {
|
|
399
|
+
set.forEach(function(handler) {
|
|
400
|
+
try {
|
|
401
|
+
handler(payload);
|
|
402
|
+
} catch (err) {
|
|
403
|
+
console.error("[AudioManager] Error in event listener for ".concat(event, ":"), err);
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
{
|
|
410
|
+
key: "getGlobalVolume",
|
|
411
|
+
value: function getGlobalVolume() {
|
|
412
|
+
return this.globalVolume;
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
key: "setGlobalVolume",
|
|
417
|
+
value: function setGlobalVolume(volume) {
|
|
418
|
+
this.globalVolume = volume;
|
|
419
|
+
this.HowlerInstance.volume(volume);
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
{
|
|
423
|
+
key: "mute",
|
|
424
|
+
value: function mute(muted) {
|
|
425
|
+
this.HowlerInstance.mute(muted);
|
|
426
|
+
}
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
key: "stopAll",
|
|
430
|
+
value: function stopAll() {
|
|
431
|
+
this.HowlerInstance.stop();
|
|
432
|
+
this.activeUrlHowl = null;
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
key: "destroy",
|
|
437
|
+
value: function destroy() {
|
|
438
|
+
this.effectCache.forEach(function(howl) {
|
|
439
|
+
return howl.unload();
|
|
440
|
+
});
|
|
441
|
+
this.urlAudioCache.forEach(function(howl) {
|
|
442
|
+
return howl.unload();
|
|
443
|
+
});
|
|
444
|
+
this.effectCache.clear();
|
|
445
|
+
this.urlAudioCache.clear();
|
|
446
|
+
this.preloadPromises.clear();
|
|
447
|
+
this.listeners.clear();
|
|
448
|
+
this.activeUrlHowl = null;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
]);
|
|
452
|
+
return AudioManager;
|
|
453
|
+
}();
|
|
454
|
+
// src/react.tsx
|
|
455
|
+
import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
|
|
456
|
+
import { jsx } from "react/jsx-runtime";
|
|
457
|
+
var AudioManagerContext = createContext(null);
|
|
458
|
+
function AudioManagerProvider(param) {
|
|
459
|
+
var manager = param.manager, children = param.children;
|
|
460
|
+
return /* @__PURE__ */ jsx(AudioManagerContext.Provider, {
|
|
461
|
+
value: manager,
|
|
462
|
+
children: children
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
function useAudio() {
|
|
466
|
+
var manager = useContext(AudioManagerContext);
|
|
467
|
+
if (!manager) {
|
|
468
|
+
throw new Error("useAudio must be used within an <AudioManagerProvider>.");
|
|
469
|
+
}
|
|
470
|
+
return manager;
|
|
471
|
+
}
|
|
472
|
+
function useAudioEvent(event, handler) {
|
|
473
|
+
var manager = useAudio();
|
|
474
|
+
var handlerRef = useRef(handler);
|
|
475
|
+
handlerRef.current = handler;
|
|
476
|
+
useEffect(function() {
|
|
477
|
+
var subscription = manager.on(event, function(payload) {
|
|
478
|
+
return handlerRef.current(payload);
|
|
479
|
+
});
|
|
480
|
+
return function() {
|
|
481
|
+
return subscription.destroy();
|
|
482
|
+
};
|
|
483
|
+
}, [
|
|
484
|
+
manager,
|
|
485
|
+
event
|
|
486
|
+
]);
|
|
487
|
+
}
|
|
488
|
+
function useGlobalVolume() {
|
|
489
|
+
var manager = useAudio();
|
|
490
|
+
var _useState = _sliced_to_array(useState(function() {
|
|
491
|
+
return manager.getGlobalVolume();
|
|
492
|
+
}), 2), volume = _useState[0], setVolumeState = _useState[1];
|
|
493
|
+
var setVolume = useCallback(function(next) {
|
|
494
|
+
manager.setGlobalVolume(next);
|
|
495
|
+
setVolumeState(next);
|
|
496
|
+
}, [
|
|
497
|
+
manager
|
|
498
|
+
]);
|
|
499
|
+
return [
|
|
500
|
+
volume,
|
|
501
|
+
setVolume
|
|
502
|
+
];
|
|
503
|
+
}
|
|
504
|
+
function usePreload(urls) {
|
|
505
|
+
var manager = useAudio();
|
|
506
|
+
var urlsKey = JSON.stringify(urls);
|
|
507
|
+
var _useState = _sliced_to_array(useState({
|
|
508
|
+
status: "idle",
|
|
509
|
+
error: null
|
|
510
|
+
}), 2), state = _useState[0], setState = _useState[1];
|
|
511
|
+
useEffect(function() {
|
|
512
|
+
var list = JSON.parse(urlsKey);
|
|
513
|
+
if (list.length === 0) {
|
|
514
|
+
setState({
|
|
515
|
+
status: "idle",
|
|
516
|
+
error: null
|
|
517
|
+
});
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
var cancelled = false;
|
|
521
|
+
setState({
|
|
522
|
+
status: "loading",
|
|
523
|
+
error: null
|
|
524
|
+
});
|
|
525
|
+
manager.preloadUrls(list).then(function() {
|
|
526
|
+
if (!cancelled) setState({
|
|
527
|
+
status: "loaded",
|
|
528
|
+
error: null
|
|
529
|
+
});
|
|
530
|
+
}, function(error) {
|
|
531
|
+
if (!cancelled) setState({
|
|
532
|
+
status: "error",
|
|
533
|
+
error: error
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
return function() {
|
|
537
|
+
cancelled = true;
|
|
538
|
+
};
|
|
539
|
+
}, [
|
|
540
|
+
manager,
|
|
541
|
+
urlsKey
|
|
542
|
+
]);
|
|
543
|
+
return state;
|
|
544
|
+
}
|
|
545
|
+
export { AudioManager, AudioManagerProvider, AudioManager as default, useAudio, useAudioEvent, useGlobalVolume, usePreload };
|
|
546
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/afei/github/howler-audio-manager/dist/index.esm.js","../src/manager.ts","../src/react.tsx"],"names":["Howl","Howler","AudioManager","options","inject","effectCache","Map","urlAudioCache","preloadPromises","activeUrlHowl","globalVolume","listeners","HowlConstructor","HowlClass","HowlerInstance","HowlerObj","defaultVolume","setGlobalVolume","hasEffect","key","has","loadEffect","url","get","unload","howl","src","preload","on","emit","type","_id","error","set","loadEffects","map","Object","keys","forEach","typedKey","isNaN","Number","playEffect","console","warn","String","playing","stop","volume","off","once","soundId","onStart","onEnd","onStop","onError","play","stopEffect","removeEffect","delete","preloadUrl","existing","state","Promise","resolve","pending","createUrlHowl","promise","reject","settle","then","preloadUrls","urls","all","playUrl","isSingleton","singleton","loop","stopUrl","event","handler","Set","add","destroy","payload","err","getGlobalVolume","mute","muted","stopAll","clear","createContext","useCallback","useContext","useEffect","useRef","useState","jsx","AudioManagerContext","AudioManagerProvider","manager","children","Provider","value","useAudio","Error","useAudioEvent","handlerRef","current","subscription","useGlobalVolume","setVolumeState","setVolume","next","usePreload","urlsKey","JSON","stringify","status","setState","list","parse","length","cancelled","default"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;CAOC,GAGD,iBAAiB;ACVjB,SAASA,IAAA,EAAMC,MAAA,QAAc,SAAA;AAWtB,IAAMC,6BAAN;;aAAMA,aAYTC,OAAA,EACAC,MAAA;gCAbSF;;QACX,IAAA,CAAQG,WAAA,GAAc,aAAA,GAAA,IAAIC;QAC1B,IAAA,CAAQC,aAAA,GAAgB,aAAA,GAAA,IAAID;QAC5B,IAAA,CAAQE,eAAA,GAAkB,aAAA,GAAA,IAAIF;QAC9B,IAAA,CAAQG,aAAA,GAA6B;QACrC,IAAA,CAAQC,YAAA,GAAe;QACvB,IAAA,CAAQC,SAAA,GAAY,aAAA,GAAA,IAAIL;QAStB,IAAA,CAAKM,eAAA,WAAkBR,mBAAAA,6BAAAA,OAAQS,SAAA,uCAAab;QAC5C,IAAA,CAAKc,cAAA,YAAiBV,mBAAAA,6BAAAA,OAAQW,SAAA,yCAAad;QAC3C,IAAI,QAAOE,oBAAAA,8BAAAA,QAASa,aAAA,MAAkB,UAAU;YAC9C,IAAA,CAAKC,eAAA,CAAgBd,QAAQa,aAAa;QAC5C;;;;YAGFE,KAAAA;mBAAAA,SAAAA,UAAUC,GAAA;gBACR,OAAO,IAAA,CAAKd,WAAA,CAAYe,GAAA,CAAID;YAC9B;;;YAEAE,KAAAA;mBAAAA,SAAAA,WAAWF,GAAA,EAAQG,GAAA;;gBACjB,IAAI,IAAA,CAAKjB,WAAA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WtF,OAAO;QAAK;QAE1CoE,QAAQ7B,WAAA,CAAYiD,MAAMlD,IAAA,CACxB;YACE,IAAI,CAACqD,WAAWJ,SAAS;gBAAED,QAAQ;gBAAUtF,OAAO;YAAK;QAC3D,GACA,SAACA;YACC,IAAI,CAAC2F,WAAWJ,SAAS;gBAAED,QAAQ;gBAAStF,OAAAA;YAAM;QACpD;QAGF,OAAO;YACL2F,YAAY;QACd;IACF,GAAG;QAACvB;QAASe;KAAQ;IAErB,OAAOrD;AACT;AFmOA,SACE5D,YAAY,EACZiG,oBAAoB,EACpBjG,gBAAgB0H,OAAO,EACvBpB,QAAQ,EACRE,aAAa,EACbI,eAAe,EACfI,UAAU,GACV","sourcesContent":["/*!\n * name: @jswork/howler-audio-manager\n * description: Howler.js audio manager, enum-driven sounds for H5.\n * homepage: https://js.work\n * version: 1.0.2\n * date: 2026-10-20 20:03:06\n * license: MIT\n */\n\n\n// src/manager.ts\nimport { Howl, Howler } from \"howler\";\nvar AudioManager = class {\n constructor(options, inject) {\n this.effectCache = /* @__PURE__ */ new Map();\n this.urlAudioCache = /* @__PURE__ */ new Map();\n this.preloadPromises = /* @__PURE__ */ new Map();\n this.activeUrlHowl = null;\n this.globalVolume = 1;\n this.listeners = /* @__PURE__ */ new Map();\n this.HowlConstructor = inject?.HowlClass ?? Howl;\n this.HowlerInstance = inject?.HowlerObj ?? Howler;\n if (typeof options?.defaultVolume === \"number\") {\n this.setGlobalVolume(options.defaultVolume);\n }\n }\n hasEffect(key) {\n return this.effectCache.has(key);\n }\n loadEffect(key, url) {\n if (this.effectCache.has(key)) {\n this.effectCache.get(key).unload();\n }\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on(\"load\", () => {\n this.emit(\"load\", { type: \"effect\", key, url });\n });\n howl.on(\"loaderror\", (_id, error) => {\n this.emit(\"error\", { type: \"effect\", key, url, error });\n });\n this.effectCache.set(key, howl);\n }\n loadEffects(map) {\n Object.keys(map).forEach((key) => {\n const typedKey = isNaN(Number(key)) ? key : Number(key);\n this.loadEffect(typedKey, map[key]);\n });\n }\n playEffect(key, options) {\n const howl = this.effectCache.get(key);\n if (!howl) {\n console.warn(`[AudioManager] Effect not found for key: ${String(key)}.`);\n return void 0;\n }\n if (howl.playing()) {\n howl.stop();\n }\n if (typeof options?.volume === \"number\") {\n howl.volume(options.volume);\n }\n howl.off(\"play\");\n howl.off(\"end\");\n howl.off(\"stop\");\n howl.off(\"playerror\");\n howl.once(\"play\", (soundId) => {\n options?.onStart?.(soundId);\n this.emit(\"play\", { type: \"effect\", key, soundId });\n });\n howl.once(\"end\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit(\"end\", { type: \"effect\", key, soundId });\n });\n howl.once(\"stop\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit(\"stop\", { type: \"effect\", key, soundId });\n });\n howl.once(\"playerror\", (soundId, error) => {\n options?.onError?.(soundId, error);\n this.emit(\"error\", { type: \"effect\", key, soundId, error });\n });\n return howl.play();\n }\n stopEffect(key) {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.stop();\n }\n }\n removeEffect(key) {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.unload();\n this.effectCache.delete(key);\n }\n }\n preloadUrl(url) {\n const existing = this.urlAudioCache.get(url);\n if (existing && existing.state() === \"loaded\") {\n return Promise.resolve();\n }\n const pending = this.preloadPromises.get(url);\n if (pending) {\n return pending;\n }\n const howl = existing && existing.state() !== \"unloaded\" ? existing : this.createUrlHowl(url);\n const promise = new Promise((resolve, reject) => {\n howl.once(\"load\", () => {\n resolve();\n });\n howl.once(\"loaderror\", (_id, error) => {\n this.urlAudioCache.delete(url);\n reject(error);\n });\n });\n this.preloadPromises.set(url, promise);\n const settle = () => {\n if (this.preloadPromises.get(url) === promise) {\n this.preloadPromises.delete(url);\n }\n };\n promise.then(settle, settle);\n return promise;\n }\n preloadUrls(urls) {\n return Promise.all(urls.map((url) => this.preloadUrl(url)));\n }\n createUrlHowl(url) {\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on(\"load\", () => {\n this.emit(\"load\", { type: \"url\", url });\n });\n howl.on(\"loaderror\", (_id, error) => {\n this.urlAudioCache.delete(url);\n this.emit(\"error\", { type: \"url\", url, error });\n });\n this.urlAudioCache.set(url, howl);\n return howl;\n }\n playUrl(url, options) {\n const howl = this.urlAudioCache.get(url) || this.createUrlHowl(url);\n const isSingleton = options?.singleton !== false;\n if (isSingleton && this.activeUrlHowl && this.activeUrlHowl !== howl && this.activeUrlHowl.playing()) {\n this.activeUrlHowl.stop();\n }\n if (howl.playing()) {\n howl.stop();\n }\n if (options?.loop !== void 0) {\n howl.loop(options.loop);\n }\n if (typeof options?.volume === \"number\") {\n howl.volume(options.volume);\n }\n if (isSingleton) {\n this.activeUrlHowl = howl;\n }\n howl.off(\"play\");\n howl.off(\"end\");\n howl.off(\"stop\");\n howl.off(\"playerror\");\n howl.once(\"play\", (soundId) => {\n options?.onStart?.(soundId);\n this.emit(\"play\", { type: \"url\", url, soundId });\n });\n howl.once(\"end\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit(\"end\", { type: \"url\", url, soundId });\n if (this.activeUrlHowl === howl && !howl.loop()) {\n this.activeUrlHowl = null;\n }\n });\n howl.once(\"stop\", (soundId) => {\n if (typeof options?.volume === \"number\") {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit(\"stop\", { type: \"url\", url, soundId });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n howl.once(\"playerror\", (soundId, error) => {\n options?.onError?.(soundId, error);\n this.emit(\"error\", { type: \"url\", url, soundId, error });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n return howl.play();\n }\n stopUrl(url) {\n const howl = this.urlAudioCache.get(url);\n if (howl) {\n howl.stop();\n }\n }\n on(event, handler) {\n const set = this.listeners.get(event) ?? /* @__PURE__ */ new Set();\n this.listeners.set(event, set);\n set.add(handler);\n return {\n destroy: () => {\n set.delete(handler);\n }\n };\n }\n emit(event, payload) {\n const set = this.listeners.get(event);\n if (set) {\n set.forEach((handler) => {\n try {\n handler(payload);\n } catch (err) {\n console.error(`[AudioManager] Error in event listener for ${event}:`, err);\n }\n });\n }\n }\n getGlobalVolume() {\n return this.globalVolume;\n }\n setGlobalVolume(volume) {\n this.globalVolume = volume;\n this.HowlerInstance.volume(volume);\n }\n mute(muted) {\n this.HowlerInstance.mute(muted);\n }\n stopAll() {\n this.HowlerInstance.stop();\n this.activeUrlHowl = null;\n }\n destroy() {\n this.effectCache.forEach((howl) => howl.unload());\n this.urlAudioCache.forEach((howl) => howl.unload());\n this.effectCache.clear();\n this.urlAudioCache.clear();\n this.preloadPromises.clear();\n this.listeners.clear();\n this.activeUrlHowl = null;\n }\n};\n\n// src/react.tsx\nimport { createContext, useCallback, useContext, useEffect, useRef, useState } from \"react\";\nimport { jsx } from \"react/jsx-runtime\";\nvar AudioManagerContext = createContext(null);\nfunction AudioManagerProvider({\n manager,\n children\n}) {\n return /* @__PURE__ */ jsx(AudioManagerContext.Provider, { value: manager, children });\n}\nfunction useAudio() {\n const manager = useContext(AudioManagerContext);\n if (!manager) {\n throw new Error(\"useAudio must be used within an <AudioManagerProvider>.\");\n }\n return manager;\n}\nfunction useAudioEvent(event, handler) {\n const manager = useAudio();\n const handlerRef = useRef(handler);\n handlerRef.current = handler;\n useEffect(() => {\n const subscription = manager.on(event, (payload) => handlerRef.current(payload));\n return () => subscription.destroy();\n }, [manager, event]);\n}\nfunction useGlobalVolume() {\n const manager = useAudio();\n const [volume, setVolumeState] = useState(() => manager.getGlobalVolume());\n const setVolume = useCallback(\n (next) => {\n manager.setGlobalVolume(next);\n setVolumeState(next);\n },\n [manager]\n );\n return [volume, setVolume];\n}\nfunction usePreload(urls) {\n const manager = useAudio();\n const urlsKey = JSON.stringify(urls);\n const [state, setState] = useState({ status: \"idle\", error: null });\n useEffect(() => {\n const list = JSON.parse(urlsKey);\n if (list.length === 0) {\n setState({ status: \"idle\", error: null });\n return;\n }\n let cancelled = false;\n setState({ status: \"loading\", error: null });\n manager.preloadUrls(list).then(\n () => {\n if (!cancelled) setState({ status: \"loaded\", error: null });\n },\n (error) => {\n if (!cancelled) setState({ status: \"error\", error });\n }\n );\n return () => {\n cancelled = true;\n };\n }, [manager, urlsKey]);\n return state;\n}\nexport {\n AudioManager,\n AudioManagerProvider,\n AudioManager as default,\n useAudio,\n useAudioEvent,\n useGlobalVolume,\n usePreload\n};\n","import { Howl, Howler } from 'howler';\nimport type {\n AudioManagerOptions,\n PlayOptions,\n PlayUrlOptions,\n AudioEventType,\n AudioEventPayload,\n EventSubscription,\n SoundResourceMap\n} from './types';\n\nexport class AudioManager<K extends string | number = string> {\n private effectCache = new Map<K, Howl>();\n private urlAudioCache = new Map<string, Howl>();\n private preloadPromises = new Map<string, Promise<void>>();\n private activeUrlHowl: Howl | null = null;\n private globalVolume = 1.0;\n private listeners = new Map<AudioEventType, Set<(payload: AudioEventPayload<K>) => void>>();\n\n private HowlConstructor: typeof Howl;\n private HowlerInstance: typeof Howler;\n\n constructor(\n options?: AudioManagerOptions,\n inject?: { HowlClass?: typeof Howl; HowlerObj?: typeof Howler }\n ) {\n this.HowlConstructor = inject?.HowlClass ?? Howl;\n this.HowlerInstance = inject?.HowlerObj ?? Howler;\n if (typeof options?.defaultVolume === 'number') {\n this.setGlobalVolume(options.defaultVolume);\n }\n }\n\n hasEffect(key: K): boolean {\n return this.effectCache.has(key);\n }\n\n loadEffect(key: K, url: string): void {\n if (this.effectCache.has(key)) {\n this.effectCache.get(key)!.unload();\n }\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on('load', () => {\n this.emit('load', { type: 'effect', key, url });\n });\n howl.on('loaderror', (_id: number, error: unknown) => {\n this.emit('error', { type: 'effect', key, url, error });\n });\n this.effectCache.set(key, howl);\n }\n\n loadEffects(map: SoundResourceMap<K>): void {\n Object.keys(map).forEach((key) => {\n const typedKey = (isNaN(Number(key)) ? key : Number(key)) as K;\n this.loadEffect(typedKey, map[key as any]);\n });\n }\n\n playEffect(key: K, options?: PlayOptions): number | undefined {\n const howl = this.effectCache.get(key);\n if (!howl) {\n console.warn(`[AudioManager] Effect not found for key: ${String(key)}.`);\n return undefined;\n }\n\n if (howl.playing()) {\n howl.stop();\n }\n\n if (typeof options?.volume === 'number') {\n howl.volume(options.volume);\n }\n\n // Clear handlers from previous plays to avoid stale/duplicate callbacks.\n howl.off('play');\n howl.off('end');\n howl.off('stop');\n howl.off('playerror');\n\n howl.once('play', (soundId: number) => {\n options?.onStart?.(soundId);\n this.emit('play', { type: 'effect', key, soundId });\n });\n\n howl.once('end', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit('end', { type: 'effect', key, soundId });\n });\n\n howl.once('stop', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit('stop', { type: 'effect', key, soundId });\n });\n\n howl.once('playerror', (soundId: number, error: unknown) => {\n options?.onError?.(soundId, error);\n this.emit('error', { type: 'effect', key, soundId, error });\n });\n\n return howl.play();\n }\n\n stopEffect(key: K): void {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.stop();\n }\n }\n\n removeEffect(key: K): void {\n const howl = this.effectCache.get(key);\n if (howl) {\n howl.unload();\n this.effectCache.delete(key);\n }\n }\n\n preloadUrl(url: string): Promise<void> {\n const existing = this.urlAudioCache.get(url);\n if (existing && existing.state() === 'loaded') {\n return Promise.resolve();\n }\n\n const pending = this.preloadPromises.get(url);\n if (pending) {\n return pending;\n }\n\n // A howl in a terminal failed state (unloaded after error) must be replaced\n // so a fresh instance can retry the load.\n const howl = existing && existing.state() !== 'unloaded' ? existing : this.createUrlHowl(url);\n\n const promise = new Promise<void>((resolve, reject) => {\n howl.once('load', () => {\n resolve();\n });\n\n howl.once('loaderror', (_id: number, error: unknown) => {\n this.urlAudioCache.delete(url);\n reject(error);\n });\n });\n\n this.preloadPromises.set(url, promise);\n const settle = () => {\n if (this.preloadPromises.get(url) === promise) {\n this.preloadPromises.delete(url);\n }\n };\n promise.then(settle, settle);\n\n return promise;\n }\n\n preloadUrls(urls: string[]): Promise<void[]> {\n return Promise.all(urls.map((url) => this.preloadUrl(url)));\n }\n\n private createUrlHowl(url: string): Howl {\n const howl = new this.HowlConstructor({\n src: [url],\n preload: true\n });\n howl.on('load', () => {\n this.emit('load', { type: 'url', url });\n });\n howl.on('loaderror', (_id: number, error: unknown) => {\n this.urlAudioCache.delete(url);\n this.emit('error', { type: 'url', url, error });\n });\n this.urlAudioCache.set(url, howl);\n return howl;\n }\n\n playUrl(url: string, options?: PlayUrlOptions): number {\n const howl = this.urlAudioCache.get(url) || this.createUrlHowl(url);\n\n // Singleton mutex: stop currently active URL audio if singleton !== false\n const isSingleton = options?.singleton !== false;\n if (\n isSingleton &&\n this.activeUrlHowl &&\n this.activeUrlHowl !== howl &&\n this.activeUrlHowl.playing()\n ) {\n this.activeUrlHowl.stop();\n }\n\n // Overlap prevention on the same audio instance\n if (howl.playing()) {\n howl.stop();\n }\n\n if (options?.loop !== undefined) {\n howl.loop(options.loop);\n }\n\n if (typeof options?.volume === 'number') {\n howl.volume(options.volume);\n }\n\n if (isSingleton) {\n this.activeUrlHowl = howl;\n }\n\n // Clear handlers from previous plays to avoid stale/duplicate callbacks.\n howl.off('play');\n howl.off('end');\n howl.off('stop');\n howl.off('playerror');\n\n howl.once('play', (soundId: number) => {\n options?.onStart?.(soundId);\n this.emit('play', { type: 'url', url, soundId });\n });\n\n howl.once('end', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onEnd?.(soundId);\n this.emit('end', { type: 'url', url, soundId });\n if (this.activeUrlHowl === howl && !howl.loop()) {\n this.activeUrlHowl = null;\n }\n });\n\n howl.once('stop', (soundId: number) => {\n if (typeof options?.volume === 'number') {\n howl.volume(1);\n }\n options?.onStop?.(soundId);\n this.emit('stop', { type: 'url', url, soundId });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n\n howl.once('playerror', (soundId: number, error: unknown) => {\n options?.onError?.(soundId, error);\n this.emit('error', { type: 'url', url, soundId, error });\n if (this.activeUrlHowl === howl) {\n this.activeUrlHowl = null;\n }\n });\n\n return howl.play();\n }\n\n stopUrl(url: string): void {\n const howl = this.urlAudioCache.get(url);\n if (howl) {\n howl.stop();\n }\n }\n\n on(event: AudioEventType, handler: (payload: AudioEventPayload<K>) => void): EventSubscription {\n const set = this.listeners.get(event) ?? new Set<(payload: AudioEventPayload<K>) => void>();\n this.listeners.set(event, set);\n set.add(handler);\n return {\n destroy: () => {\n set.delete(handler);\n }\n };\n }\n\n protected emit(event: AudioEventType, payload: AudioEventPayload<K>): void {\n const set = this.listeners.get(event);\n if (set) {\n set.forEach((handler) => {\n try {\n handler(payload);\n } catch (err) {\n console.error(`[AudioManager] Error in event listener for ${event}:`, err);\n }\n });\n }\n }\n\n getGlobalVolume(): number {\n return this.globalVolume;\n }\n\n setGlobalVolume(volume: number): void {\n this.globalVolume = volume;\n this.HowlerInstance.volume(volume);\n }\n\n mute(muted: boolean): void {\n this.HowlerInstance.mute(muted);\n }\n\n stopAll(): void {\n this.HowlerInstance.stop();\n this.activeUrlHowl = null;\n }\n\n destroy(): void {\n this.effectCache.forEach((howl) => howl.unload());\n this.urlAudioCache.forEach((howl) => howl.unload());\n this.effectCache.clear();\n this.urlAudioCache.clear();\n this.preloadPromises.clear();\n this.listeners.clear();\n this.activeUrlHowl = null;\n }\n}\n","import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';\nimport type { ReactNode } from 'react';\nimport { AudioManager } from './manager';\nimport type { AudioEventType, AudioEventPayload } from './types';\n\nconst AudioManagerContext = createContext<AudioManager<any> | null>(null);\n\nexport interface AudioManagerProviderProps<K extends string | number = string> {\n manager: AudioManager<K>;\n children?: ReactNode;\n}\n\nexport function AudioManagerProvider<K extends string | number = string>({\n manager,\n children\n}: AudioManagerProviderProps<K>) {\n return (\n <AudioManagerContext.Provider value={manager}>\n {children}\n </AudioManagerContext.Provider>\n );\n}\n\nexport function useAudio<K extends string | number = string>(): AudioManager<K> {\n const manager = useContext(AudioManagerContext);\n if (!manager) {\n throw new Error('useAudio must be used within an <AudioManagerProvider>.');\n }\n return manager as unknown as AudioManager<K>;\n}\n\nexport function useAudioEvent<K extends string | number = string>(\n event: AudioEventType,\n handler: (payload: AudioEventPayload<K>) => void\n): void {\n const manager = useAudio<K>();\n const handlerRef = useRef(handler);\n handlerRef.current = handler;\n\n useEffect(() => {\n const subscription = manager.on(event, (payload) => handlerRef.current(payload));\n return () => subscription.destroy();\n }, [manager, event]);\n}\n\nexport function useGlobalVolume(): [number, (volume: number) => void] {\n const manager = useAudio();\n const [volume, setVolumeState] = useState(() => manager.getGlobalVolume());\n\n const setVolume = useCallback(\n (next: number) => {\n manager.setGlobalVolume(next);\n setVolumeState(next);\n },\n [manager]\n );\n\n return [volume, setVolume];\n}\n\nexport interface PreloadState {\n status: 'idle' | 'loading' | 'loaded' | 'error';\n error: unknown;\n}\n\nexport function usePreload(urls: string[]): PreloadState {\n const manager = useAudio();\n const urlsKey = JSON.stringify(urls);\n const [state, setState] = useState<PreloadState>({ status: 'idle', error: null });\n\n useEffect(() => {\n const list: string[] = JSON.parse(urlsKey);\n if (list.length === 0) {\n setState({ status: 'idle', error: null });\n return;\n }\n\n let cancelled = false;\n setState({ status: 'loading', error: null });\n\n manager.preloadUrls(list).then(\n () => {\n if (!cancelled) setState({ status: 'loaded', error: null });\n },\n (error) => {\n if (!cancelled) setState({ status: 'error', error });\n }\n );\n\n return () => {\n cancelled = true;\n };\n }, [manager, urlsKey]);\n\n return state;\n}\n\n\n"]}
|