--- name: expo-audio description: Guide for using expo-audio to implement audio playback and recording in React Native apps. Apply when working with audio features, sound playback, recording, or text-to-speech functionality. allowed-tools: Read, Edit, Write, Grep, Glob --- # Expo Audio (expo-audio) Guide for using `expo-audio` to implement audio playback and recording in React Native apps. ## Overview - **Package**: `expo-audio` (replaces deprecated `expo-av`) - **Platform**: Android, iOS, tvOS, Web - **Bundled version**: ~1.1.1 - **Documentation**: https://docs.expo.dev/versions/latest/sdk/audio/ ## Installation ```bash npx expo install expo-audio ``` ## Configuration ### app.json Plugin Configuration Add the `expo-audio` plugin to your `app.json`: ```json { "expo": { "plugins": [ [ "expo-audio", { "microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone.", "recordAudioAndroid": true } ] ] } } ``` **Configurable properties:** - `microphonePermission` (iOS only): String for NSMicrophoneUsageDescription. Set to `false` to disable. - `recordAudioAndroid` (Android only): Boolean to enable RECORD_AUDIO permission (default: `true`) ### Background Audio (iOS) For background audio playback on iOS, add `UIBackgroundModes` to `app.json`: ```json { "expo": { "ios": { "infoPlist": { "UIBackgroundModes": ["audio"] } } } } ``` ## Core Concepts ### AudioPlayer The `AudioPlayer` class handles audio playback. You can create players using: - `useAudioPlayer()` hook (recommended for React components) - `createAudioPlayer()` function (for imperative usage outside components) ### AudioRecorder The `AudioRecorder` class handles audio recording. Use: - `useAudioRecorder()` hook (recommended for React components) ## Usage Patterns ### Playing Sounds (React Hook - Recommended) Use `useAudioPlayer` hook in React components: ```tsx import { Button, View } from "react-native"; import { useAudioPlayer } from "expo-audio"; const audioSource = require("./assets/sound.mp3"); export default function App() { const player = useAudioPlayer(audioSource); return (