Expo Audio (expo-av)
A library that provides an API to implement audio playback and recording in apps.
Deprecated: TheAudiocomponent fromexpo-av, which is documented on this page, has now been deprecated and replaced by an improved version inexpo-audio. Learn aboutexpo-audio.
Audio from expo-av allows you to implement audio playback and recording in your app.
Note that audio automatically stops if headphones/bluetooth audio devices are disconnected.
See the playlist example app for an example on the media playback API, and the recording example app for an example of the recording API.
Installation
- npx expo install expo-avIf you are installing this in an existing React Native app, make sure to install expo in your project.
Usage
Playing sounds
import { useEffect, useState } from 'react'; import { View, StyleSheet, Button } from 'react-native'; import { Audio } from 'expo-av'; export default function App() { const [sound, setSound] = useState(); async function playSound() { console.log('Loading Sound'); const { sound } = await Audio.Sound.createAsync( require('./assets/Hello.mp3') ); setSound(sound); console.log('Playing Sound'); await sound.playAsync(); } useEffect(() => { return sound ? () => { console.log('Unloading Sound'); sound.unloadAsync(); } : undefined; }, [sound]); return ( <View style={styles.container}> <Button title="Play Sound" onPress={playSound} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', backgroundColor: '#ecf0f1', padding: 10, }, });
Recording sounds
import { useState } from 'react'; import { View, StyleSheet, Button } from 'react-native'; import { Audio } from 'expo-av'; export default function App() { const [recording, setRecording] = useState(); const [permissionResponse, requestPermission] = Audio.usePermissions(); async function startRecording() { try { if (permissionResponse.status !== 'granted') { console.log('Requesting permission..'); await requestPermission(); } await Audio.setAudioModeAsync({ allowsRecordingIOS: true, playsInSilentModeIOS: true, }); console.log('Starting recording..'); const { recording } = await Audio.Recording.createAsync( Audio.RecordingOptionsPresets.HIGH_QUALITY ); setRecording(recording); console.log('Recording started'); } catch (err) { console.error('Failed to start recording', err); } } async function stopRecording() { console.log('Stopping recording..'); setRecording(undefined); await recording.stopAndUnloadAsync(); await Audio.setAudioModeAsync( { allowsRecordingIOS: false, } ); const uri = recording.getURI(); console.log('Recording stopped and stored at', uri); } return ( <View style={styles.container}> <Button title={recording ? 'Stop Recording' : 'Start Recording'} onPress={recording ? stopRecording : startRecording} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', backgroundColor: '#ecf0f1', padding: 10, }, });
Playing or recording audio in background iOS
On iOS, audio playback and recording in background is only available in standalone apps, and it requires some extra configuration.
On iOS, each background feature requires a special key in UIBackgroundModes array in your Info.plist file.
In standalone apps this array is empty by default, so to use background features you will need to add appropriate keys to your app.json configuration.
See an example of app.json that enables audio playback in background:
{ "expo": { ... "ios": { ... "infoPlist": { ... "UIBackgroundModes": [ "audio" ] } } } }
Notes on web usage
- A MediaRecorder issue on Chrome produces WebM files missing the duration metadata. See the open Chromium issue.
- MediaRecorder encoding options and other configurations are inconsistent across browsers, utilizing a Polyfill such as kbumsik/opus-media-recorder or ai/audio-recorder-polyfill in your application will improve your experience. Any options passed to
prepareToRecordAsyncwill be passed directly to the MediaRecorder API and as such the polyfill. - Web browsers require sites to be served securely for them to listen to a mic. See MediaDevices
getUserMedia()security for more details.
API
import { Audio } from 'expo-av';
No API data file found, sorry!
Unified API
The rest of the API on the Sound.Audio is the same as the API for Video component ref. See the AV documentation for more information.