I'm working with AudioWorkletNode
and, to use it, it requires you to first load a processor module on the audio_context.audioWorklet
.
DOMException: Failed to construct 'AudioWorkletNode': AudioWorkletNode cannot be created: AudioWorklet does not have a valid AudioWorkletGlobalScope. Load a script via audioWorklet.addModule() first.
Due to the modular nature of my code, I need a reliable way to prevent the unnecessary multiple loading of the processor module; not to .addModule
if already done to a given AudioContext
. Is there such a way other than try
- catch
ing and marking on the AudioContext
object?
Let's say you have named your processor my-processor
and the definition of your AudioWorkletProcessor
looks somehow like this:
class MyProcessor extends AudioWorkletProcessor {
process () {
return true;
}
}
registerProcessor('my-processor', MyProcessor);
To check if that processor is already loaded you can do something like this from within the main thread:
// somewhere inside of an async function ...
let audioWorkletNode;
try {
audioWorkletNode = new AudioWorkletNode(audioContext, 'my-processor');
} catch (err) {
await audioContext.audioWorklet.addModule('./worklet.js');
audioWorkletNode = new AudioWorkletNode(audioContext, 'my-processor');
}
The code is using a try
/ catch
block but it does at least not load the JS file if it was already loaded before.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.