AudioEncoder
TheAudioEncoder class encodes raw AudioData objects into compressed EncodedAudioChunk objects. It supports common audio codecs like AAC, MP3, Opus, FLAC, and Vorbis through FFmpeg.
This class follows the W3C WebCodecs AudioEncoder specification.
Quick Example
Supported Audio Codecs
Constructor
Creates a newAudioEncoder with output and error callbacks.
Initialization callbacks for the encoder.
TypeError if callbacks are not functions.
Example: Creating an encoder
Example: Creating an encoder
Properties
Current encoder state. One of:
'unconfigured'- Not configured yet, orreset()was called'configured'- Ready to encode audio data'closed'- Encoder has been closed and cannot be used
Number of pending encode operations. Useful for backpressure management
when encoding large amounts of audio data.
Static Methods
isConfigSupported()
Check if an audio encoder configuration is supported on this platform.Configuration to test for support.
Promise<AudioEncoderSupport> - Resolves with support status and normalized config.
Example: Checking codec support
Example: Checking codec support
Example: Fallback codec selection
Example: Fallback codec selection
Instance Methods
configure()
Configure the encoder with codec parameters. Must be called before encoding.Encoder configuration specifying codec and audio parameters.
InvalidStateErrorif encoder is closedNotSupportedErrorif codec is not supported
Example: Configuring for AAC
Example: Configuring for AAC
Example: Configuring for Opus
Example: Configuring for Opus
encode()
Queue anAudioData object for encoding.
The raw audio data to encode. Must have a valid format and timestamp.
InvalidStateErrorif encoder is not configuredTypeErrorif data is invalid
Example: Encoding audio frames
Example: Encoding audio frames
Example: Backpressure handling
Example: Backpressure handling
addEventListener()
Add an event listener for encoder events.Event type. Currently supports
'dequeue' which fires when the encode queue size decreases.Callback function to invoke when the event fires.
Event listener options.
Example: Listening for dequeue events
Example: Listening for dequeue events
removeEventListener()
Remove a previously added event listener.Event type (e.g.,
'dequeue').The callback function to remove. Must be the same function reference that was added.
Example: Removing a listener
Example: Removing a listener
flush()
Wait for all pending encode operations to complete.Promise<void> - Resolves when all queued audio data has been encoded.
Throws:
InvalidStateErrorif encoder is not configured- Rejects if encoding fails
Example: Flushing the encoder
Example: Flushing the encoder
reset()
Reset the encoder to the unconfigured state.'unconfigured' state.
You must call configure() again before encoding more audio.
Throws: InvalidStateError if encoder is closed
Example: Resetting and reconfiguring
Example: Resetting and reconfiguring
close()
Close the encoder and release all resources.close(). Any pending encode operations
are aborted and the state transitions to 'closed'.
Example: Proper cleanup
Example: Proper cleanup
Type Definitions
AudioEncoderConfig
Configuration options for the audio encoder.AudioEncoderInit
Initialization callbacks for creating an AudioEncoder.AudioEncoderOutputMetadata
Metadata provided with encoded audio chunks.AudioEncoderSupport
Result fromisConfigSupported() indicating codec support.
AudioBitrateMode
Bitrate control mode for audio encoding.See Also
AudioData
Raw audio sample data for encoding
AudioDecoder
Decode EncodedAudioChunks back to AudioData
EncodedAudioChunk
Encoded audio chunk output