在电子中使用 AudioWorklet (DOMException: The user aborted a reques

2023-01-28前端开发问题
516

本文介绍了在电子中使用 AudioWorklet (DOMException: The user aborted a request)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在我的电子应用程序中使用 AudioWorklet 进行计量等.在开发模式下执行时工作正常,其中工作集由诸如 http://localhost:3000/processor.js.但是,如果我尝试在 prod 模式下运行该应用程序,则该文件将在本地提供,例如 file://tmp/etc/etc/build/processor.js 并且在开发人员控制台中,我什至可以看到该文件正在正确预览,但我收到此错误消息:

I am trying to use an AudioWorklet within my electron app for metering etc. which works fine when executed in dev mode where the worklet is being served by an express dev server like http://localhost:3000/processor.js. However if I try to run the app in prod mode the file is being served locally like file://tmp/etc/etc/build/processor.js and in the developer-console I can even see the file correctly being previewed but I get this error message:

Uncaught (in promise) DOMException: The user aborted a request.

Uncaught (in promise) DOMException: The user aborted a request.

在在这里但不幸的是,我在堆栈溢出方面的声誉还不够高,无法直接发表评论.将 mime 类型更改为 application/javascript 或 text/javascript 的建议听起来不错,但我不知道如何强制电子对特定文件使用特定的 mime 类型.此外,在网络选项卡的开发者控制台中,似乎 chromium 实际上已经为我的 processor.js 假设了一个 javascript 文件.

I saw that someone else had a similar problem before over here but unfortunately my reputation on stack overflow is not high enough to comment directly. The suggestion there to change the mime-type to application/javascript or text/javascript sounds good but I have no idea how to force electron to use a specific mime-type for a specific file. Furthermore in the developer-console in the network tab it seems like chromium is actually already assuming a javascript file for my processor.js.

我已经尝试使用类似的自定义协议加载工作集

I already tried to load the worklet with a custom protocol like that

protocol.registerStandardSchemes(['worklet']);

app.on('ready', () => {
  protocol.registerHttpProtocol('worklet', (req, cb) => {
    fs.readFile(req.url.replace('worklet://', ''), (err, data) => {
      cb({ mimeType: 'text/javascript', data });
    });
  });
});

然后在添加worklet时

and then when adding the worklet

await ctx.audioWorklet.addModule('worklet://processor.js');

不幸的是,这只以这些错误结束,然后是第一个错误

unfortunately this only ends in these errors followed by the first error

GET worklet://processor.js/0 ()
未捕获的错误:您提供的错误不包含堆栈跟踪.
...

GET worklet://processor.js/ 0 ()
Uncaught Error: The error you provided does not contain a stack trace.
...

推荐答案

如果有人感兴趣,我找到了一个 hacky 解决方案.为了强制使用 mime 类型的电子/铬,我很高兴我将带有文件 api 作为字符串的 worklet 文件加载,将其转换为具有 mime 类型 text/javascript 的 blob,然后从中创建一个对象 url

I found a hacky solution if anybody is interested. To force a mime-type electron / chromium is happy with I load the worklet file with the file api as a string, convert it to a blob with mime-type text/javascript and then create an object url from that

const processorPath = isDevMode ? 'public/processor.js' : `${global.__dirname}/processor.js`;
const processorSource = await readFile(processorPath); // just a promisified version of fs.readFile
const processorBlob = new Blob([processorSource.toString()], { type: 'text/javascript' });
const processorURL = URL.createObjectURL(processorBlob);
await ctx.audioWorklet.addModule(processorURL);

希望这对遇到同样问题的人有所帮助...

Hope this helps anyone having the same problem...

这篇关于在电子中使用 AudioWorklet (DOMException: The user aborted a request)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13