Audio
Provides basic sample playback.
We designed the Audio API to be as non-intrusive as possible, so audio will automatically stop if other audio starts playing on the device, if the device is locked, if the app / experience is backgrounded, or if headphones / bluetooth audio devices are disconnected.
Try the playlist example app (source code is on GitHub) to see an example usage of this API.
Enabling Audio
Audio is disabled by default, so your app must enable it explicitly to play sounds.
Exponent.Audio.setIsEnabled(value)
Arguments
-
value (boolean) —
trueenables Expo Audio, andfalsedisables it.
Returns
A Promise that will reject if audio playback could not be enabled for the device.
Playing sounds
Exponent.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
Exponent.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 Exponent.Audio.Sound.
Example
const sound = new Exponent.Audio.Sound({
source: require('./assets/sounds/hello.mp3'),
});
-
soundInstance.loadAsync()Loads the sound into memory and prepares it for playing. This must be called before calling
play.Returns
A
Promisethat is fulfilled when the sound is loaded, or rejects if loading failed. -
soundInstance.unload()Unloads the sound.
loadAsyncmust be called again in order to be able to play the sound. -
soundInstance.isLoaded()Returns
A
booleanthat is true if and only if the sound is loaded. -
soundInstance.getDurationMillis()Returns
The duration of the sound in milliseconds. This is available only after the sound is loaded.
-
soundInstance.play()Plays the sound.
Returns
A
Promisethat is resolved, once the sound starts playing, with thestatusof the sound (seegetStatusfor details). -
soundInstance.pause()Pauses the sound.
Returns
A
Promisethat is resolved, once playback is paused, with thestatusof the sound (seegetStatusfor details). -
soundInstance.stop()Stops the sound.
Returns
A
Promisethat is resolved, once playback is stopped, with thestatusof the sound (seegetStatusfor details). -
soundInstance.setPosition(millis)Sets the playback position of the sound.Parameters
-
millis (number) — The position to seek the sound to.
Returns
A
Promisethat is resolved, once the seek occurs, with thestatusof the sound (seegetStatusfor details). -
soundInstance.setVolume(value)Sets the volume of the sound. This is NOT the system volume, and will only affect this sound. This value defaults to
1.Parameters
-
value (number) — A number between
0(silence) and1(maximum volume).
Returns
A
Promisethat is resolved, once the volume is set, with thestatusof the sound (seegetStatusfor details). -
-
soundInstance.setIsMuted(value)Sets whether the sound is muted. This is independent of the volume of the sound set in
setVolume. This also does not affect the system volume, and only pertains to this sound. This value defaults totrue.Parameters
-
value (boolean) —
truemutes the sound, andfalseunmutes it.Returns
A
Promisethat is resolved, once the mute state is set, with thestatusof the sound (seegetStatusfor details). -
soundInstance.setIsLooping(value)Sets whether playback of the sound should loop. When
true, it will loop indefinitely. This value defaults tofalse.Parameters
-
value (boolean) —
truesets the sound to loop indefinitely.
Returns
A
Promisethat is resolved, once the loop state is set, with thestatusof the sound (seegetStatusfor details). -
-
soundInstance.getStatus()Gets the
statusof the sound.Returns
A
Promisethat is resolved with thestatusof the sound: a dictionary with the following key-value pairs.-
position_millis: the current position of playback in milliseconds. -
is_playing: a boolean describing if the sound is currently playing. -
is_muted: a boolean describing if the sound is currently muted. -
is_looping: a boolean describing if the sound is currently looping.
-
-
soundInstance.setStatusChangeCallback(callback)Sets a function to be called at regular intervals with the
statusof the Sound. SeegetStatusfor details onstatus, and seesetStatusPollingTimeoutMillisfor details on the regularity with which this function is called.Parameters
-
callback (function) — A function taking a single parameter
status(a dictionary, described ingetStatus).
-
-
soundInstance.setStatusPollingTimeoutMillis(millis)Sets the interval with which the status change callback is called. See
setStatusChangeCallbackfor details on the status change callback. This value defaults to 100 milliseconds.Note that the status change callback will automatically be called when another call to the API for this sound completes (such as
play,pause, orstop) regardless of this value.Parameters
-
millis (number) — The new interval between calls of the status change callback.
-
-
soundInstance.setPlaybackFinishedCallback(callback)Sets a function to be called whenever this sound is finished playing to the end. This callback is not called when looping is enabled, or when the sound is stopped or paused before it finishes playing.
Parameters
-
callback (function) — The callback receives no parameters.
-
© Copyright 2017, Expo.