Skip to main content
Version: Next

Decoding

You can decode audio data independently, without creating an AudioContext, using the exported functions decodeAudioData, decodePCMInBase64, and getAudioDuration.

If you already have an audio context, you can decode audio data directly using its decodeAudioData function; the decoded audio will then be automatically resampled to match the context's sampleRate.

caution

Supported file formats:

  • flac
  • mp3
  • ogg
  • opus
  • wav
  • aac
  • m4a
  • mp4
  • aif
  • aiff
  • caf (ios)

Methods

decodeAudioData

Decodes audio data from a file path, an ArrayBuffer, or a bundled app asset (a number module id returned by require('./audio.mp3')). The optional sampleRate parameter lets you resample the decoded audio; if not provided, the original sample rate from the file is used.

ParameterTypeDescription
inputArrayBufferArrayBuffer with encoded audio data.
stringPath to a remote or local audio file.
number
Mobile only
React Native asset module id (for example, the value returned by require('./audio.mp3')).
sampleRate
Optional
numberTarget sample rate for the decoded audio.
fetchOptions
Optional
RequestInitAdditional fetch options when input is a remote URL (for example, auth headers).
caution
If you pass a number as input, decoding resolves the bundled asset through React Native's Image component. By default, only .mp3, .wav, .mp4, .m4a, and .aac assets are supported. To use other formats, see static non-image resources.

Returns: Promise<AudioBuffer>

Example decoding remote URL
import { decodeAudioData } from 'react-native-audio-api';

const url = ... // url to an audio

const buffer = await decodeAudioData(url);

getAudioDuration

Reads the duration of an audio source without decoding the full file into an AudioBuffer. Use this when you only need the encoded file duration. Use decodeAudioData when you need sample data for playback or processing.

On mobile, duration is read from file metadata via the native decoder. Local paths, file:// URIs, remote http(s): URLs, and ArrayBuffer input are supported. Remote URL probing requires FFmpeg in the native build — see Runtime flags. Asset module ids, base64 data URLs, and blob URLs are rejected explicitly.

On web, this API uses browser audio metadata loading and supports sources the browser can load, such as remote URLs, relative URLs, blob URLs, and data URLs.

There is no sampleRate option because the returned duration belongs to the encoded source and does not depend on a caller-selected output sample rate.

ParameterTypeDescription
inputArrayBufferEncoded audio bytes (mobile only).
stringLocal path, file:// URI, or remote URL.
fetchOptions
Optional
RequestInitAdditional fetch options when input is a remote URL (for example, auth headers).

Returns Promise<number> with the duration in seconds.

Throws an error when duration metadata is unavailable or when the input type is not supported.

Example reading local file duration

import { getAudioDuration } from 'react-native-audio-api';

const duration = await getAudioDuration('file:///tmp/recording.wav');

Example reading remote URL duration

import { getAudioDuration } from 'react-native-audio-api';

const duration = await getAudioDuration('https://example.com/audio.mp3', {
headers: { Authorization: 'Bearer token' },
});

decodePCMInBase64

Decodes base64-encoded PCM audio data.

ParameterTypeDescription
base64StringstringBase64-encoded PCM audio data.
inputSampleRatenumberSample rate of the input PCM data.
inputChannelCountnumberNumber of channels in the input PCM data.
isInterleaved
Optional
booleanWhether the PCM data is interleaved. Defaults to true.

Returns Promise<AudioBuffer>

Example decoding with data in base64 format

const data = ... // data encoded in base64 string
// data is interleaved (Channel1, Channel2, Channel1, Channel2, ...)
const buffer = await decodeAudioData(data, 4800, 2, true);