大网数据广告活动
Web 标准与前端开发

AudioWorkletGlobalScope: registerProcessor() method

· 52CE 我爱测

The **`registerProcessor`** method of the AudioWorkletGlobalScope interface registers a class constructor derived from AudioWorkletProcessor interface under a specified _name_.

Web Audio API

The registerProcessor method of the AudioWorkletGlobalScope interface registers a class constructor derived from AudioWorkletProcessor interface under a specified name.

Syntax

registerProcessor(name, processorCtor)

Parameters

  • name
    • : A string representing the name under which the processor will be registered.
  • processorCtor
    • : The constructor of a class derived from AudioWorkletProcessor.

[!NOTE] A key-value pair { name: constructor } is saved internally in the AudioWorkletGlobalScope once the processor is registered. The name is to be referred to when creating an AudioWorkletNode based on the registered processor. A new processor by the given name is internally created and associated with the new node.

Return value

None (undefined).

Exceptions

  • NotSupportedError DOMException

    • : Thrown under the following conditions:
      • The name is an empty string.
      • A constructor under the given name is already registered. Registering the same name twice is not allowed.
  • TypeError

    • : Thrown under the following conditions:
      • The processorCtor is not a callable constructor.
      • The AudioWorkletProcessor.parameterDescriptors property of the constructor exists and doesn't return an array of AudioParamDescriptor-based objects.

Examples

In this example we create a custom AudioWorkletNode that outputs silence.

First, we need to define a custom AudioWorkletProcessor and register it. Note that this should be done in a separate file.

// test-processor.js
class TestProcessor extends AudioWorkletProcessor {
  process(inputs, outputs, parameters) {
    return true;
  }
}

registerProcessor("test-processor", TestProcessor);

Next, in our main script file we'll load the processor, create an instance of AudioWorkletNode — passing it the processor name that we used when calling registerProcessor — and connect it to an audio graph.

const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule("test-processor.js");
const node = new AudioWorkletNode(audioContext, "test-processor");
node.connect(audioContext.destination);

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。