AudioDecoder
TheAudioDecoder class decodes compressed audio data (EncodedAudioChunk) into raw audio samples (AudioData). It supports common audio codecs like AAC, MP3, Opus, and FLAC.
This class follows the W3C WebCodecs AudioDecoder specification.
Quick Example
Constructor
Creates a newAudioDecoder instance.
Initialization object containing callback functions.
Properties
Current state of the decoder. One of:
'unconfigured'- Initial state, callconfigure()before decoding'configured'- Ready to decode chunks'closed'- Decoder has been closed and cannot be used
Number of chunks waiting to be decoded. Use this for backpressure management -
if the queue grows too large, pause feeding new chunks until it decreases.
Static Methods
isConfigSupported()
Checks whether a given configuration is supported by the decoder without creating an instance.The configuration to test for support.
Promise<AudioDecoderSupport> - Object indicating whether the config is supported.
Example: Check codec support
Example: Check codec support
Instance Methods
configure()
Configures the decoder with codec parameters. Must be called beforedecode().
Configuration object specifying the codec and audio parameters.
InvalidStateError if the decoder is closed.
Example: Configure for AAC-LC
Example: Configure for AAC-LC
Example: Configure with AudioSpecificConfig
Example: Configure with AudioSpecificConfig
decode()
Queues an encoded audio chunk for decoding. Decoded frames will be delivered asynchronously to theoutput callback.
The encoded audio chunk to decode. Unlike video, most audio chunks are keyframes
and can be decoded independently.
InvalidStateErrorif the decoder is not configuredDataErrorif the chunk data is malformed
Example: Decode audio stream
Example: Decode audio stream
flush()
Completes all pending decode operations. Call this when you’ve submitted all chunks and need to ensure all output has been delivered.Promise<void> - Resolves when all queued chunks have been decoded and output.
Throws: InvalidStateError if the decoder is not configured.
Example: Flush before closing
Example: Flush before closing
reset()
Resets the decoder to the'unconfigured' state, discarding any pending work. The decoder can be reconfigured after reset.
InvalidStateError if the decoder is closed.
Example: Reset and reconfigure
Example: Reset and reconfigure
close()
Closes the decoder and releases all resources. The decoder cannot be used after callingclose().
Example: Proper cleanup
Example: Proper cleanup
Interfaces
AudioDecoderConfig
Configuration for the audio decoder.AudioDecoderInit
Initialization object for creating an AudioDecoder.AudioDecoderSupport
Result fromisConfigSupported().
Common Codec Strings
Complete Decoding Example
See Also
AudioData
Raw audio sample data produced by decoding
AudioEncoder
Encode AudioData into EncodedAudioChunks
EncodedAudioChunk
Compressed audio data for decoding
Codec Registry
Full list of supported codecs