expo-audio
Version: 55.0.6
A cross-platform audio library for React Native and Expo apps. Provides playback and recording capabilities with support for various audio formats.
Installation
npx expo install expo-audio
Usage
import { AudioPlayer } from 'expo-audio';
import { useState } from 'react';
import { Button, View } from 'react-native';
function App() {
const [player, setPlayer] = useState<AudioPlayer | null>(null);
async function playSound() {
const audioPlayer = new AudioPlayer(require('./assets/sound.mp3'));
setPlayer(audioPlayer);
await audioPlayer.play();
}
return (
<View>
<Button title="Play Sound" onPress={playSound} />
</View>
);
}
API Reference
AudioPlayer
The main class for playing audio files.
Creates a new audio player instanceParameters:
source: Audio source (require() or URI string)
const player = new AudioPlayer(require('./audio.mp3'));
// or
const player = new AudioPlayer('https://example.com/audio.mp3');
Starts or resumes playback
Stops playback and resets position
player.seekTo(position)
(position: number) => Promise<void>
Seeks to specified position in millisecondsawait player.seekTo(30000); // Seek to 30 seconds
player.setVolume(volume)
(volume: number) => Promise<void>
Sets playback volume (0.0 to 1.0)await player.setVolume(0.5); // 50% volume
Releases resources associated with the player
AudioRecorder
new AudioRecorder(options)
Creates a new audio recorder instanceimport { AudioRecorder } from 'expo-audio';
const recorder = new AudioRecorder({
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 128000,
});
Stops recording and returns the URI of the recorded fileconst uri = await recorder.stop();
console.log('Recorded audio:', uri);
Permissions
requestRecordingPermissionsAsync()
() => Promise<PermissionResponse>
Asks the user to grant permissions for audio recordingimport { requestRecordingPermissionsAsync } from 'expo-audio';
const { status } = await requestRecordingPermissionsAsync();
if (status === 'granted') {
// Permission granted
}
getRecordingPermissionsAsync()
() => Promise<PermissionResponse>
Checks the current recording permission status
Examples
Audio Player with Controls
import { AudioPlayer } from 'expo-audio';
import { useState, useEffect } from 'react';
import { View, Button, Text, StyleSheet } from 'react-native';
function AudioPlayerExample() {
const [player, setPlayer] = useState<AudioPlayer | null>(null);
const [isPlaying, setIsPlaying] = useState(false);
useEffect(() => {
return () => {
player?.release();
};
}, [player]);
async function loadAndPlay() {
const audioPlayer = new AudioPlayer(
'https://example.com/audio.mp3'
);
setPlayer(audioPlayer);
await audioPlayer.play();
setIsPlaying(true);
}
async function togglePlayback() {
if (!player) return;
if (isPlaying) {
await player.pause();
setIsPlaying(false);
} else {
await player.play();
setIsPlaying(true);
}
}
return (
<View style={styles.container}>
<Button title="Load Audio" onPress={loadAndPlay} />
{player && (
<Button
title={isPlaying ? 'Pause' : 'Play'}
onPress={togglePlayback}
/>
)}
</View>
);
}
const styles = StyleSheet.create({
container: { padding: 20, gap: 10 },
});
Audio Recorder
import { AudioRecorder, requestRecordingPermissionsAsync } from 'expo-audio';
import { useState } from 'react';
import { View, Button } from 'react-native';
function RecorderExample() {
const [recorder] = useState(() => new AudioRecorder());
const [recording, setRecording] = useState(false);
async function startRecording() {
const { status } = await requestRecordingPermissionsAsync();
if (status !== 'granted') {
alert('Permission required');
return;
}
await recorder.record();
setRecording(true);
}
async function stopRecording() {
const uri = await recorder.stop();
setRecording(false);
console.log('Recording saved to:', uri);
}
return (
<View>
<Button
title={recording ? 'Stop Recording' : 'Start Recording'}
onPress={recording ? stopRecording : startRecording}
/>
</View>
);
}
| Platform | Supported |
|---|
| iOS | ✅ |
| Android | ✅ |
| Web | ✅ |
Permissions
iOS
Add to app.json:
{
"expo": {
"plugins": [
[
"expo-audio",
{
"microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone."
}
]
]
}
}
Android
Permissions are automatically added to AndroidManifest.xml:
RECORD_AUDIO - for recording audio
Resources