DEV Community

Leo Kalshteyn
Leo Kalshteyn

Posted on

Expo's Audio API

In looking up ways to implement audio to a react native app I am making, I found that expo has an API that allows playback and recording of audio. I am using it for strictly playing sound effects during certain actions in a small game but There quite extensive recording options among others that this API offers. This API also has video features but that is for another blog.

Installation

expo install expo-av

In components:

import { Audio } from 'expo-av';

There are also configurations for iOS and Andriod which you can see here: https://github.com/expo/expo/tree/master/packages/expo-av

Usage for Playing Sounds

To play sounds you need to set an object to Audio.sound which represents a sound tied to an asset sound file or url.

const soundObject = new Audio.Sound();

There are also parameters:

  • source (object / number / Asset): Source of the sound.
  • initialStatus (PlaybackStatusToSet): The initial PlaybackStatusToSet of the sound
  • onPlaybackStatusUpdate (function): A function taking a single parameter PlaybackStatus.
  • downloadFirst (boolean): Default value true, and when true, the system will attempt to download the resource to the device before loading.
const soundObject = new Audio.Sound();
soundObject.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);
await soundObject.loadAsync(source, initialStatus, downloadFirst);
Enter fullscreen mode Exit fullscreen mode

This example creates and loads a sound from source, with optional initialStatus, onPlaybackStatusUpdate, and downloadFirst.

Returns:

A Promise that is rejected if creation failed, or fulfilled with the following dictionary if creation succeeded:

  • sound : the newly created and loaded Sound object.
  • status : the PlaybackStatus of the Sound object.
try {
  const { sound: soundObject, status } = await Audio.Sound.createAsync(
    require('./assets/sounds/hello.mp3'),
    { shouldPlay: true }
  );
Enter fullscreen mode Exit fullscreen mode

There are far more which you can see here: https://docs.expo.io/versions/latest/sdk/audio/

Usage for Recording Sounds

After creating an instance of this class, prepareToRecordAsync must be called in order to record audio. Once recording is finished, call stopAndUnloadAsync. Audio recording permissions must also given for recording. Set an object to Audio.Recording

const recording = new Audio.Recording();
try {
  await recording.prepareToRecordAsync(Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY);
  await recording.startAsync();
  // You are now recording!
Enter fullscreen mode Exit fullscreen mode

Returns:

A Promise that is resolved with the status of the Recording: a dictionary with the following key-value pairs.Before prepareToRecordAsync is called, the status will be as follows:

  • canRecord : a boolean set to false.
  • isDoneRecording : a boolean set to false.

After prepareToRecordAsync() is called, but before stopAndUnloadAsync() is called, the status will be as follows:

  • canRecord : a boolean set to true.
  • isRecording : a boolean describing if the Recording is currently recording.
  • durationMillis : the current duration of the recorded audio.

After stopAndUnloadAsync() is called, the status will be as follows:

  • canRecord : a boolean set to false.
  • isDoneRecording : a boolean set to true. = durationMillis : the final duration of the recorded audio.

This is just a glimpse of the various features. With recording options, you can go much more in-depth and customize bit rate, quality etc.

References

Top comments (1)

Collapse
 
gauravmavani29 profile image
GauravMavani29

Hello can any one tell me static file is playing on mobile successfully but if i pass through URL sound is not coming i mean its not getting loaded.....in laptop bot are working good in react-native expo..... expo-av