Skip to content
Wyatt Kirby edited this page Sep 24, 2024 · 3 revisions

constructor(filename, basePath, onError, options)

filename {string} Either absolute or relative path to the sound file. On android, absolute path starts with '/sdcard/', so if you want to access a sound called "my_sound.mp3" on Downloads folder, the absolute path will be: '/sdcard/Downloads/my_sound.mp3'.

basePath {?string} Optional base path of the file. Omit this or pass '' if filename is an absolute path. Otherwise, you may use one of the predefined directories: Sound.MAIN_BUNDLE, Sound.DOCUMENT, Sound.LIBRARY, Sound.CACHES.

onError {?function(error, props)} Optional callback function. If the file is successfully loaded, the first parameter error is null, and props contains an object with two properties: duration (in seconds) and numberOfChannels (1 for mono and 2 for stereo sound), both of which can also be accessed from the Sound instance object. If an initialization error is encountered (e.g. file not found), error will be an object containing code, description, and the stack trace.

options {?object} Platform-specific options:

Windows Only: enableSMTCIntegration {?boolean}. Optional setting for windows to enable or disable SMTC integration (controlling your apps sounds or music via the keyboard, and the built-in media controls on Windows.) This is enabled by default. Set this to false when you don't want users to be able to control your sounds (e.g. sound effects.) See the Windows.Media.SystemMediaTransportControls documentation for more information.

isLoaded()

Return true if the sound has been loaded.

play(onEnd)

onEnd {?function(successfully)} Optional callback function that gets called when the playback finishes successfully or an audio decoding error interrupts it.

pause(callback)

callback {?function()} Optional callback function that gets called when the sound has been paused.

Pause the sound.

stop(callback)

callback {?function()} Optional callback function that gets called when the sound has been stopped.

Stop playback and set the seek position to 0.

reset()

Reset the audio player to its uninitialized state (Android only)

release()

Release the audio player resource associated with the instance.

getDuration()

Return the duration in seconds, or -1 before the sound gets loaded.

getNumberOfChannels()

Return the number of channels (1 for mono and 2 for stereo sound), or -1 before the sound gets loaded.

getVolume()

Return the volume of the audio player (not the system-wide volume), ranging from 0.0 (silence) through 1.0 (full volume, the default).

setVolume(value)

value {number} Set the volume, ranging from 0.0 (silence) through 1.0 (full volume).

getSystemVolume(callback)

callback {?function(systemVolume)} Return the system-wide volume of the audio player, ranging from 0.0 (silence) through 1.0 (full volume). (Android only).

setSystemVolume(value)

value {number} Set the system-side volume, ranging from 0.0 (silence) through 1.0 (full volume). (Android only).

getPan()

Return the stereo pan position of the audio player (not the system-wide pan), ranging from -1.0 (full left) through 1.0 (full right). The default value is 0.0 (center).

setPan(value)

value {number} Set the pan, ranging from -1.0 (full left) through 1.0 (full right).

getNumberOfLoops()

Return the loop count of the audio player. The default is 0 which means to play the sound once. A positive number specifies the number of times to return to the start and play again. A negative number indicates an indefinite loop.

setNumberOfLoops(value)

value {number} Set the loop count. 0 means to play the sound once. A positive number specifies the number of times to return to the start and play again (iOS only). A negative number indicates an indefinite loop (iOS and Android).

getCurrentTime(callback)

callback {function(seconds, isPlaying)} Callback will receive the current playback position in seconds and whether the sound is being played.

setCurrentTime(value)

value {number} Seek to a particular playback point in seconds.

setSpeed(value)

value {number} Speed of the audio playback (iOS Only).

setSpeakerphoneOn(value)

speaker {boolean} Sets the speakerphone on or off (Android only).

It requires this permission in your AndroidManifest: <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

enableInSilenceMode(enabled) (deprecated)

enabled {boolean} Whether to enable playback in silence mode (iOS only).

Use the static method Sound.setCategory('Playback') instead which has the same effect.

setCategory(value) (deprecated)

Deprecated. Use the static method Sound.setCategory instead.

Static Methods

Sound.setCategory(value, mixWithOthers) (iOS & Android only)

value {string} Sets AVAudioSession category, which allows playing sound in background, stop sound playback when phone is locked, etc. Parameter options: "Ambient", "SoloAmbient", "Playback", "Record", "PlayAndRecord", "AudioProcessing", "MultiRoute".

More info about each category can be found in https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html

mixWithOthers {boolean} can be set to true to force mixing with other audio sessions.

To play sound in the background, make sure to add the following to the Info.plist file.

<key>UIBackgroundModes</key>
<array>
  <string>audio</string>
</array>

Sound.setMode(value) (iOS only)

value {string} Sets AVAudioSession mode, which works in conjunction with the category to determine audio mixing behavior. Parameter options: "Default", "VoiceChat", "VideoChat", "GameChat", "VideoRecording", "Measurement", "MoviePlayback", "SpokenAudio".

This should be called in conjunction with Sound.setCategory.

More info about each mode can be found in https://developer.apple.com/documentation/avfoundation/avaudiosession/audio_session_modes

Sound.setActive(value) (iOS only)

Sets AVAudioSession as active, which is recommended on iOS to achieve seamless background playback. Use this method to deactivate the AVAudioSession when playback is finished in order for other apps to regain access to the audio stack.