createMicrophoneAnalyzer

View as Markdown

createMicrophoneAnalyzer

  • Const createMicrophoneAnalyzer(options): Promise<MicrophoneAnalyzer>

Initializes a microphone analyzer. You can use a MicrophoneAnalyzer to track the input audio volume.

To stop the analyzer, call the destroy() method on the returned object.

The returned object emits the following events:

  • volumeChanged: Instantaneous volume from 0 to 100.
  • destroyed: The object has been destroyed. The parameter describes the reason: null (if you called destroy()), "error" (in case of errors), or "disconnected" (if the device was disconnected).

Parameters

options
string | MediaStream | MediaTrackConstraintsRequired

Either the id of the device to analyze, a MediaStreamConstraints object, or a MediaStream.

Returns

Promise<MicrophoneAnalyzer>

Example

1const micAnalyzer = await createMicrophoneAnalyzer("device-id");
2
3micAnalyzer.on("volumeChanged", (vol) => {
4 console.log("Volume: ", vol);
5});
6micAnalyzer.on("destroyed", (reason) => {
7 console.log("Microphone analyzer destroyed", reason);
8});
9
10micAnalyzer.destroy();