@94ai/nf-audio 8.3.14 → 8.3.15

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.
@@ -296,15 +296,33 @@ const _sfc_main = {
296
296
  return window.location.protocol === "https:" || window.location.hostname === "localhost";
297
297
  },
298
298
  async startAudioCtx() {
299
- var _a, _b;
299
+ var _a, _b, _c, _d;
300
300
  if (this.isHighPrecision()) {
301
301
  try {
302
302
  if (!this.audioCtx) {
303
- this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
303
+ const AudioCtx = window.AudioContext || window.webkitAudioContext;
304
+ this.audioCtx = new AudioCtx();
305
+ if (this.audioCtx.state === "suspended") {
306
+ await this.audioCtx.resume();
307
+ }
304
308
  this.audioSource = this.audioCtx.createMediaElementSource(this.audio);
305
- await ((_a = this.audioCtx.audioWorklet) == null ? void 0 : _a.addModule("data:application/javascript," + encodeURIComponent(this.audioWorkletProcessor)));
306
- this.processor = new AudioWorkletNode(this.audioCtx, "tick");
307
- this.audioSource.connect(this.processor).connect(this.audioCtx.destination);
309
+ const blob = new Blob([this.audioWorkletProcessor], {
310
+ type: "application/javascript"
311
+ });
312
+ const workletUrl = URL.createObjectURL(blob);
313
+ try {
314
+ await ((_a = this.audioCtx.audioWorklet) == null ? void 0 : _a.addModule(workletUrl));
315
+ this.processor = new AudioWorkletNode(this.audioCtx, "tick");
316
+ this.audioSource.connect(this.processor).connect(this.audioCtx.destination);
317
+ } catch (workletErr) {
318
+ if (this.debuglog) {
319
+ console.warn("AudioWorklet unavailable, falling back to direct output:", workletErr);
320
+ }
321
+ this.audioSource.connect(this.audioCtx.destination);
322
+ this.processor = null;
323
+ } finally {
324
+ URL.revokeObjectURL(workletUrl);
325
+ }
308
326
  if ((_b = this.processor) == null ? void 0 : _b.port) {
309
327
  this.processor.port.onmessage = (event) => {
310
328
  if (event.data.command === "tick") {
@@ -322,12 +340,12 @@ const _sfc_main = {
322
340
  };
323
341
  }
324
342
  } else {
325
- this.audioCtx.resume();
343
+ await this.audioCtx.resume();
326
344
  }
327
345
  } catch (e) {
328
346
  console.log(e);
329
347
  }
330
- this.processor.port.postMessage({
348
+ (_d = (_c = this.processor) == null ? void 0 : _c.port) == null ? void 0 : _d.postMessage({
331
349
  command: "start"
332
350
  });
333
351
  }
@@ -294,15 +294,33 @@ const _sfc_main = {
294
294
  return window.location.protocol === "https:" || window.location.hostname === "localhost";
295
295
  },
296
296
  async startAudioCtx() {
297
- var _a, _b;
297
+ var _a, _b, _c, _d;
298
298
  if (this.isHighPrecision()) {
299
299
  try {
300
300
  if (!this.audioCtx) {
301
- this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
301
+ const AudioCtx = window.AudioContext || window.webkitAudioContext;
302
+ this.audioCtx = new AudioCtx();
303
+ if (this.audioCtx.state === "suspended") {
304
+ await this.audioCtx.resume();
305
+ }
302
306
  this.audioSource = this.audioCtx.createMediaElementSource(this.audio);
303
- await ((_a = this.audioCtx.audioWorklet) == null ? void 0 : _a.addModule("data:application/javascript," + encodeURIComponent(this.audioWorkletProcessor)));
304
- this.processor = new AudioWorkletNode(this.audioCtx, "tick");
305
- this.audioSource.connect(this.processor).connect(this.audioCtx.destination);
307
+ const blob = new Blob([this.audioWorkletProcessor], {
308
+ type: "application/javascript"
309
+ });
310
+ const workletUrl = URL.createObjectURL(blob);
311
+ try {
312
+ await ((_a = this.audioCtx.audioWorklet) == null ? void 0 : _a.addModule(workletUrl));
313
+ this.processor = new AudioWorkletNode(this.audioCtx, "tick");
314
+ this.audioSource.connect(this.processor).connect(this.audioCtx.destination);
315
+ } catch (workletErr) {
316
+ if (this.debuglog) {
317
+ console.warn("AudioWorklet unavailable, falling back to direct output:", workletErr);
318
+ }
319
+ this.audioSource.connect(this.audioCtx.destination);
320
+ this.processor = null;
321
+ } finally {
322
+ URL.revokeObjectURL(workletUrl);
323
+ }
306
324
  if ((_b = this.processor) == null ? void 0 : _b.port) {
307
325
  this.processor.port.onmessage = (event) => {
308
326
  if (event.data.command === "tick") {
@@ -320,12 +338,12 @@ const _sfc_main = {
320
338
  };
321
339
  }
322
340
  } else {
323
- this.audioCtx.resume();
341
+ await this.audioCtx.resume();
324
342
  }
325
343
  } catch (e) {
326
344
  console.log(e);
327
345
  }
328
- this.processor.port.postMessage({
346
+ (_d = (_c = this.processor) == null ? void 0 : _c.port) == null ? void 0 : _d.postMessage({
329
347
  command: "start"
330
348
  });
331
349
  }
@@ -484,11 +484,29 @@ export default {
484
484
  if (this.isHighPrecision()) {
485
485
  try {
486
486
  if (!this.audioCtx) {
487
- this.audioCtx = new (window.AudioContext || window.webkitAudioContext)()
487
+ const AudioCtx = window.AudioContext || window.webkitAudioContext
488
+ this.audioCtx = new AudioCtx()
489
+ if (this.audioCtx.state === 'suspended') {
490
+ await this.audioCtx.resume()
491
+ }
488
492
  this.audioSource = this.audioCtx.createMediaElementSource(this.audio)
489
- await this.audioCtx.audioWorklet?.addModule('data:application/javascript,' + encodeURIComponent(this.audioWorkletProcessor))
490
- this.processor = new AudioWorkletNode(this.audioCtx, 'tick')
491
- this.audioSource.connect(this.processor).connect(this.audioCtx.destination)
493
+ // 使用 Blob URL 替代 data URI,提升 Safari 兼容性
494
+ const blob = new Blob([this.audioWorkletProcessor], { type: 'application/javascript' })
495
+ const workletUrl = URL.createObjectURL(blob)
496
+ try {
497
+ await this.audioCtx.audioWorklet?.addModule(workletUrl)
498
+ this.processor = new AudioWorkletNode(this.audioCtx, 'tick')
499
+ this.audioSource.connect(this.processor).connect(this.audioCtx.destination)
500
+ } catch (workletErr) {
501
+ if (this.debuglog) {
502
+ console.warn('AudioWorklet unavailable, falling back to direct output:', workletErr)
503
+ }
504
+ // AudioWorklet 不可用时,直接连接到 destination,保证有声音
505
+ this.audioSource.connect(this.audioCtx.destination)
506
+ this.processor = null
507
+ } finally {
508
+ URL.revokeObjectURL(workletUrl)
509
+ }
492
510
  if (this.processor?.port) {
493
511
  this.processor.port.onmessage = (event) => {
494
512
  if (event.data.command === 'tick') {
@@ -506,12 +524,12 @@ export default {
506
524
  }
507
525
  }
508
526
  } else {
509
- this.audioCtx.resume()
527
+ await this.audioCtx.resume()
510
528
  }
511
529
  } catch (e) {
512
530
  console.log(e)
513
531
  }
514
- this.processor.port.postMessage({ command: 'start' })
532
+ this.processor?.port?.postMessage({ command: 'start' })
515
533
  }
516
534
  },
517
535
  pause() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@94ai/nf-audio",
3
- "version": "8.3.14",
3
+ "version": "8.3.15",
4
4
  "description": "> TODO: description",
5
5
  "keywords": [],
6
6
  "author": "liuxiangxiang <liuxiangxiang@94ai.com>",
@@ -14,8 +14,8 @@
14
14
  "url": "http://94ai.gitlab.com/zoujiahe/common-ui.git"
15
15
  },
16
16
  "dependencies": {
17
- "@94ai/nf-message": "^8.3.14",
18
- "@94ai/nf-theme-chalk": "^8.3.14",
17
+ "@94ai/nf-message": "^8.3.15",
18
+ "@94ai/nf-theme-chalk": "^8.3.15",
19
19
  "vue-demi": "^0.14.5"
20
20
  },
21
21
  "peerDependenciesMeta": {
@@ -31,5 +31,5 @@
31
31
  "types": "lib/index.d.ts",
32
32
  "main": "lib/nf-audio.cjs.js",
33
33
  "module": "lib/nf-audio.esm-bundler.js",
34
- "gitHead": "6a9daedc595c1d560ec1fdb3574fd6bf11c93ade"
34
+ "gitHead": "924ec21a035c411edfec714ef72e4cfd08c887ee"
35
35
  }