Audio
Provides basic sample playback and recording.
Note that Expo does not support backgrounding, so audio is not available to play in the background of your experience. Audio also automatically stops if headphones / bluetooth audio devices are disconnected.
Try the playlist example app (source code is on GitHub) to see an example usage of the playback API, and the recording example app (source code is on GitHub) to see an example usage of the recording API.
Enabling Audio and customizing Audio Mode
Expo.Audio.setIsEnabledAsync(value)
Audio is disabled by default, so your app must enable it explicitly to play sounds.
Arguments
-
value (boolean) —
trueenables Expo Audio, andfalsedisables it.
Returns
A Promise that will reject if audio playback could not be enabled for the device.
Expo.Audio.setAudioModeAsync(mode)
We provide this API to customize the audio experience on iOS and Android.
Arguments
-
mode (object) —
A dictionary with the following key-value pairs:
-
playsInSilentLockedModeIOS: a boolean selecting if your experience’s audio should play in silent mode or locked mode on iOS. This value defaults tofalse. -
allowsRecordingIOS: a boolean selecting if recording is enabled on iOS. This value defaults tofalse. -
interruptionModeIOS: an enum selecting how your experience’s audio should interact with the audio from other apps on iOS:-
INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS: This is the default option. If this option is set, your experience’s audio is mixed with audio playing in background apps. -
INTERRUPTION_MODE_IOS_DO_NOT_MIX: If this option is set, your experience’s audio interrupts audio from other apps. -
INTERRUPTION_MODE_IOS_DUCK_OTHERS: If this option is set, your experience’s audio lowers the volume (“ducks”) of audio from other apps while your audio plays.
-
-
shouldDuckAndroid: a boolean selecting if your experience’s audio should automatically be lowered in volume (“duck”) if audio from another app interrupts your experience. This value defaults totrue. Iffalse, audio from other apps will pause your audio. -
interruptionModeAndroid: an enum selecting how your experience’s audio should interact with the audio from other apps on Android:-
INTERRUPTION_MODE_ANDROID_DO_NOT_MIX: If this option is set, your experience’s audio interrupts audio from other apps. -
INTERRUPTION_MODE_ANDROID_DUCK_OTHERS: This is the default option. If this option is set, your experience’s audio lowers the volume (“ducks”) of audio from other apps while your audio plays.
-
-
Returns
A Promise that will reject if the audio mode could not be enabled for the device. Note that these are the only legal AudioMode combinations of (playsInSilentLockedModeIOS, allowsRecordingIOS, interruptionModeIOS), and any other will result in promise rejection:
-
false, false, INTERRUPTION_MODE_IOS_DO_NOT_MIX -
false, false, INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS -
true, true, INTERRUPTION_MODE_IOS_DO_NOT_MIX -
true, true, INTERRUPTION_MODE_IOS_DUCK_OTHERS -
true, true, INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS -
true, false, INTERRUPTION_MODE_IOS_DO_NOT_MIX -
true, false, INTERRUPTION_MODE_IOS_DUCK_OTHERS -
true, false, INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS
Playing sounds
Expo.Audio.Sound
This class represents a sound corresponding to an Asset or URL.
Parameters
-
options (object) —
A map of options:
-
source — The source of the audio data to display. The following forms are supported:
-
A string with a network URL pointing to an audio file on the web.
-
require('path/to/file')for an audio file asset in the source code directory. -
An
Expo.Assetobject for an audio file asset.
The iOS developer documentation lists the audio formats supported on iOS.
The Android developer documentation lists the audio formats supported on Android.
-
-
Returns
A newly constructed instance of Expo.Audio.Sound.
Example
const sound = new Expo.Audio.Sound({
source: require('./assets/sounds/hello.mp3'),
});
try {
await sound.loadAsync();
await sound.playAsync();
// Your sound is playing!
} catch (error) {
// An error occurred!
}
-
soundInstance.getStatusAsync()Gets the
statusof the sound.Returns
A
Promisethat is resolved with thestatusof the sound: a dictionary with the following key-value pairs.If the sound is not loaded, it will only contain a single key:
-
isLoaded: a boolean set tofalse.
Otherwise, it contains all of the following key-value pairs:
-
isLoaded: a boolean set totrue. -
isPlaying: a boolean describing if the sound is currently playing. -
durationMillis: the duration of the sound in milliseconds. -
positionMillis: the current position of playback in milliseconds. -
rate: the current rate of the sound. -
shouldCorrectPitch: a boolean describing if we are correcting the pitch for a changed rate. -
volume: the current volume of the sound. -
isMuted: a boolean describing if the sound is currently muted. -
isLooping: a boolean describing if the sound is currently looping. -
didJustFinish: a boolean describing if the sound just played to completion at the time that this status was received. When the sound plays to completion, the function passed insetCallbackis called exactly once withdidJustFinishset totrue.didJustFinishis nevertruein any other case.
-
-
soundInstance.setCallback(callback)Sets a function to be called regularly with the
statusof the sound. SeegetStatusAsyncfor details onstatus.The callback will be called when another call to the API for this sound completes (such as
playAsync,setRateAsync,getStatusAsync, orunloadAsync), and will also be called at regular intervals while the sound is loaded. CallsetCallbackPollingMillisto modify the interval with which the callback is called while loaded.Parameters
-
callback (function) — A function taking a single parameter
status(a dictionary, described ingetStatusAsync).
-
-
soundInstance.setCallbackPollingMillis(millis)Sets the interval with which the status change callback is called while the sound is loaded. See
setCallbackfor details on the status change callback. This value defaults to 100 milliseconds.Parameters
-
millis (number) — The new interval between calls of the status change callback.
-
-
soundInstance.loadAsync()Loads the sound into memory and prepares it for playing. This must be called before calling
playAsync. This method can only be called if the sound is in an unloaded state.Returns
A
Promisethat is fulfilled when the sound is loaded with thestatusof the sound (seegetStatusAsyncfor details), or rejects if loading failed. ThePromisewill also reject if the sound was already loaded. -
soundInstance.unloadAsync()Unloads the sound.
loadAsyncmust be called again in order to be able to play the sound.Returns
A
Promisethat is fulfilled when the sound is unloaded with thestatusof the sound (seegetStatusAsyncfor details), or rejects if unloading failed. -
soundInstance.playAsync()Plays the sound. This method can only be called if the sound has been loaded.
Returns
A
Promisethat is resolved, once the sound starts playing, with thestatusof the sound (seegetStatusAsyncfor details). -
soundInstance.pauseAsync()Pauses the sound. This method can only be called if the sound has been loaded.
Returns
A
Promisethat is resolved, once playback is paused, with thestatusof the sound (seegetStatusAsyncfor details). -
soundInstance.stopAsync()Stops the sound. This method can only be called if the sound has been loaded.
Returns
A
Promisethat is resolved, once playback is stopped, with thestatusof the sound (seegetStatusAsyncfor details). -
soundInstance.setPositionAsync(millis)Sets the playback position of the sound. This method can only be called if the sound has been loaded.
Parameters
-
millis (number) — The position to seek the sound to.
Returns
A
Promisethat is resolved, once the seek occurs, with thestatusof the sound (seegetStatusAsyncfor details). -
-
soundInstance.setRateAsync(value, shouldCorrectPitch)Sets the playback rate of the sound. This method can only be called if the sound has been loaded.
NOTE: This is only available on Android API version 23 and later.
Parameters
-
value (number) — The desired playback rate of the sound. This value must be between
0.0and32.0. -
shouldCorrectPitch (boolean) — If set to
true, the pitch of the sound will be corrected (so a rate different than1.0will timestretch the sound).
Returns
A
Promisethat is resolved once the rate is changed with thestatusof the sound (seegetStatusAsyncfor details). If the Android API version is less than 23, thePromisewill reject. -
-
soundInstance.setVolumeAsync(value)Sets the volume of the sound. This is NOT the system volume, and will only affect this sound. This value defaults to
1.0. This method can only be called if the sound has been loaded.Parameters
-
value (number) — A number between
0.0(silence) and1.0(maximum volume).
Returns
A
Promisethat is resolved, once the volume is set, with thestatusof the sound (seegetStatusAsyncfor details). -
-
soundInstance.setIsMutedAsync(value)Sets whether the sound is muted. This is independent of the volume of the sound set in
setVolumeAsync. This also does not affect the system volume, and only pertains to this sound. This value defaults totrue. This method can only be called if the sound has been loaded.Parameters
-
value (boolean) —
truemutes the sound, andfalseunmutes it.
Returns
A
Promisethat is resolved, once the mute state is set, with thestatusof the sound (seegetStatusAsyncfor details). -
-
soundInstance.setIsLoopingAsync(value)Sets whether playback of the sound should loop. When
true, it will loop indefinitely. This value defaults tofalse. This method can only be called if the sound has been loaded.Parameters
-
value (boolean) —
truesets the sound to loop indefinitely.
Returns
A
Promisethat is resolved, once the loop state is set, with thestatusof the sound (seegetStatusAsyncfor details). -
Recording sounds
Expo.Audio.Recording
This class represents an audio recording. After creating an instance of this class, prepareToRecordAsync must be called in order to record audio. Once recording is finished, call stopAndUnloadAsync. Note that only one recorder is allowed to exist in the state between prepareToRecordAsync and stopAndUnloadAsync at any given time.
Note that your experience must request audio recording permissions in order for recording to function. See the Permissions module for more details.
Returns
A newly constructed instance of Expo.Audio.Recording.
Example
const recording = new Expo.Audio.Recording();
try {
await recording.prepareToRecordAsync();
await recording.startAsync();
// You are now recording!
} catch (error) {
// An error occurred!
}
-
recordingInstance.getStatusAsync()Gets the
statusof theRecording.Returns
A
Promisethat is resolved with thestatusof theRecording: a dictionary with the following key-value pairs.Before
prepareToRecordAsyncis called, thestatuswill be as follows:-
canRecord: a boolean set tofalse. -
isDoneRecording: a boolean set tofalse.
After
prepareToRecordAsyncis called, but beforestopAndUnloadAsyncis called, thestatuswill be as follows:-
canRecord: a boolean set totrue. -
isRecording: a boolean describing if theRecordingis currently recording. -
durationMillis: the current duration of the recorded audio.
After
stopAndUnloadAsyncis called, thestatuswill be as follows:-
canRecord: a boolean set tofalse. -
isDoneRecording: a boolean set totrue. -
durationMillis: the final duration of the recorded audio.
-
-
recordingInstance.setCallback(callback)Sets a function to be called regularly with the
statusof theRecording. SeegetStatusAsyncfor details onstatus.The callback will be called when another call to the API for this recording completes (such as
prepareToRecordAsync,startAsync,getStatusAsync, orstopAndUnloadAsync), and will also be called at regular intervals while the recording can record. CallsetCallbackPollingMillisto modify the interval with which the callback is called while the recording can record.Parameters
-
callback (function) — A function taking a single parameter
status(a dictionary, described ingetStatusAsync).
-
-
recordingInstance.setCallbackPollingMillis(millis)Sets the interval with which the status change callback is called while the recording can record. See
setCallbackfor details on the status change callback. This value defaults to 100 milliseconds.Parameters
-
millis (number) — The new interval between calls of the status change callback.
-
-
recordingInstance.prepareToRecordAsync()Loads the recorder into memory and prepares it for recording. This must be called before calling
startAsync. This method can only be called if theRecordinginstance has never yet been prepared.Returns
A
Promisethat is fulfilled when the recorder is loaded and prepared, or rejects if this failed. If anotherRecordingexists in your experience that is currently prepared to record, thePromisewill reject. The promise is resolved with thestatusof the recording (seegetStatusAsyncfor details). -
recordingInstance.isPreparedToRecord()Returns
A
booleanthat is true if and only if theRecordingis prepared to record. -
recordingInstance.startAsync()Begins recording. This method can only be called if the
Recordinghas been prepared.Returns
A
Promisethat is fulfilled when recording has begun, or rejects if recording could not start. The promise is resolved with thestatusof the recording (seegetStatusAsyncfor details). -
recordingInstance.pauseAsync()Pauses recording. This method can only be called if the
Recordinghas been prepared.NOTE: This is only available on Android API version 24 and later.
Returns
A
Promisethat is fulfilled when recording has paused, or rejects if recording could not be paused. If the Android API version is less than 24, thePromisewill reject. The promise is resolved with thestatusof the recording (seegetStatusAsyncfor details). -
recordingInstance.stopAndUnloadAsync()Stops the recording and deallocates the recorder from memory. This reverts the
Recordinginstance to an unprepared state, and anotherRecordinginstance must be created in order to record again. This method can only be called if theRecordinghas been prepared.Returns
A
Promisethat is fulfilled when recording has stopped, or rejects if recording could not be stopped. The promise is resolved with thestatusof the recording (seegetStatusAsyncfor details). -
recordingInstance.getURI()Gets the local URI of the
Recording. Note that this will only succeed once theRecordingis prepared to record.Returns
A
stringwith the local URI of theRecording, ornullif theRecordingis not prepared to record. -
recordingInstance.getNewSound()Gets a new
Soundobject to play back theRecording. Note that this will only succeed once theRecordingis done recording (oncestopAndUnloadAsynchas been called).Returns
A
Soundcreated with the local URI of theRecording, ornullif theRecordingis not done recording.
© Copyright 2017, Expo.