Skip to main content

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

Last three formats are decoded with ffmpeg on the mobile, see for more info.

decodeAudioData

Decodes audio data from either a file path or an ArrayBuffer. 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 audio data.
stringPath to remote or local audio file.
numberAsset module id.
sampleRate
Optional
numberTarget sample rate for the decoded audio.
fetchOptions
Optional
RequestInitAdditional headers parameters when passing url to fetch.

Returns Promise<AudioBuffer>.

caution

If you are passing asset module id to decode function, bear in mind that it uses Image component provided by React Native internally. By default only support .mp3, .wav, .mp4, .m4a, .aac audio file formats. If you want to use other types, refer to this section for more info.

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. Default is 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);