Skip to main content

Audio tag (<Audio>)

React component that mimics web <Audio> behavior.

Usage

import React, { useRef } from 'react';
import { View } from 'react-native';
import { Audio, AudioTagHandle } from 'react-native-audio-api';

const DEMO_URL = 'https://example.com/audio.mp3';

export function Player() {
const ref = useRef<AudioTagHandle>(null);

return (
<View style={{ flex: 1 }}>
<Audio
ref={ref}
source={DEMO_URL}
controls
onLoad={() => console.log('ready')}
onError={(e) => console.error(e)}
onPositionChange={(seconds) => {}}
/>
</View>
);
}

Props (AudioProps)

Only required field is a source. Callbacks default to no-ops if omitted.

NameTypeDefaultDescription
sourceAudioSourceAsset id (require(...)), string URI/path, or { uri?, headers? } for HTTP(S).
contextBaseAudioContextOptional. Native: implicit AudioContext when omitted. Web: unused for HTML element playback.
autoPlaybooleanfalseStart playback after load.
controlsbooleanfalseWhen true, renders default AudioControls above children.
loopbooleanfalseLoop playback.
mutedbooleanfalseMuted state.
volumenumber1Linear volume passed to the underlying source.
preloadPreloadType'auto'Loading strategy for the source ('none', 'metadata', 'auto').
forceDownloadbooleanfalseNative only. When true, download the full remote file in JS instead of streaming via HTTP byte ranges (useful when servers rate-limit range requests). Ignored on web and for HLS (.m3u8) sources.
playbackRatenumber1Playback speed. Native playback accepts rates up to x4; negative reverse playback is not supported. No-op for HLS files playback.
preservesPitchbooleantruePreserve pitch when playbackRate changes. Set to false to let pitch change with playback speed.
onLoadStart() => voidno-opLoad started.
onLoad() => voidno-opSource decoded / graph attached.
onError(error: Error) => voidno-opNetwork, decode, or unsupported format (e.g. missing FFmpeg) errors.
onPositionChange(seconds: number) => voidno-opPlayback position updates while playing.
onEnded() => voidno-opNatural end of playback (also used internally when looping restarts).
onPlay() => voidno-opAfter play().
onPause() => voidno-opAfter pause().
onVolumeChange(volume: number) => voidno-opWhen effective volume changes.

AudioSource

  • string — URI or path (http(s):, file://, or platform-specific asset path).
  • number — Result of require('./file.mp3') (bundled asset).
  • AudioURISource{ uri?: string; headers?: Record<string, string> } for fetch with custom headers.

preload

  • none - Do not load on mount.
  • metadata - Probe duration for remote sources via native URL metadata (requires FFmpeg). When FFmpeg is disabled in the build, this behaves like none.
  • auto (and empty string) - Load source immediately on mount (default behavior).

metadata (native)

For remote http(s): sources, duration is read through the same native URL probing path as getAudioDuration. This requires an FFmpeg-enabled build — check with isFfmpegEnabled.

When FFmpeg is disabled, preload="metadata" is treated as none: the source is not fetched and duration stays unknown until playback starts.

Ref handle (AudioTagHandle) methods

play

Start or resume playback.

pause

Pause playback.

seekToTime

ParameterTypeDescription
secondsnumberSeek to a time in seconds (clamped to duration when known).

setVolume

ParameterTypeDescription
volumenumberUpdates volume state .

setMuted

ParameterTypeDescription
mutedbooleanUpdates muted state .

useAudioTagContext

Types

type AudioComponentContextType = {
play: () => void;
pause: () => void;
seekToTime: (seconds: number) => void;
setVolume: (volume: number) => void;
setMuted: (muted: boolean) => void;

ready: boolean;
volume: number;
muted: boolean;
playbackState: AudioTagPlaybackState;
currentTime: number;
duration: number;
autoPlay: boolean;
loop: boolean;
preload: PreloadType;
playbackRate: number;
preservesPitch: boolean;
};

Useful for creating your custom UI component. Must be used under <Audio>. Throws an Error if used outside the provider.

import React from 'react';
import { Button } from 'react-native';

const AudioControls: React.FC = () => {
const {
play,
pause,
seekToTime,
} = useAudioTagContext();

return (
<>
<Button onPress={() => play()}>Play</Button>
<Button onPress={() => pause()}>Pause</Button>
<Button onPress={() => seekToTime(10)}>Seek to 10</Button>
</>
);
}

const MyAudioTagWrapper : React.FC = () => {
const URL = ...;

return (
<Audio source={URL}>
<AudioControls />
</Audio>
);
}

Remarks

  • FFmpeg: HLS and several compressed formats require an FFmpeg build for URL streaming. Without FFmpeg, remote sources are downloaded and decoded with miniaudio where supported. See Runtime flags.