Skip to content

NssAudio

A simple audio player for sound effects with built-in SFX commands. NUI

Getting Started

NssAudio plays audio files from a base path. It also provides static SFX methods for common UI sounds that require no setup.

js
import {NssAudio} from "nui://nss_libs/ui/NssAudio/NssAudio.js";

const audio = new NssAudio('nui://my_resource/html/sounds/');
audio.setVolume(0.5);
audio.playAudio('click.mp3');

Constructor

new NssAudio(audio_path?, volume?)

Creates a new audio player instance.

ParamTypeDefaultDescription
audio_pathstring''Base path for audio files (e.g. 'nui://my_resource/sounds/')
volumenumber0.5Initial volume between 0 and 1
js
const audio = new NssAudio('nui://my_resource/html/sounds/', 0.25);

Methods

.playAudio(filename, prevent_reset?, loop?)

Plays an audio file. Returns a Promise that resolves when playback ends.

ParamTypeDefaultDescription
filenamestringThe audio filename (appended to the base path)
prevent_resetbooleanfalseIf true, does not reset the audio element before playing
loopbooleanfalseIf true, loops the audio indefinitely

Returns Promise<string> — resolves with the filename when playback ends.

js
audio.playAudio('notification.mp3').then((filename) => {
    console.log(filename + ' finished playing');
});

.loopAudio(filename)

Plays an audio file in a loop until stop() is called.

ParamTypeDefaultDescription
filenamestringThe audio filename

Returns Promise<string> — resolves with the filename when stopped.

js
audio.loopAudio('ambient.mp3');
// Later:
audio.stop();

.stop(force_ended_event?)

Stops the current audio playback.

ParamTypeDefaultDescription
force_ended_eventbooleanfalseIf true, fires the ended event callback even though the audio was stopped manually

Returns undefined

js
audio.stop(true); // Stop and resolve the playAudio promise

.setVolume(volume)

Sets the playback volume.

ParamTypeDefaultDescription
volumenumberVolume between 0 (mute) and 1 (full)

Returns undefined

js
audio.setVolume(0.3);

.setAudioPath(path)

Changes the base audio path.

ParamTypeDefaultDescription
pathstringThe new base path

Returns undefined

js
audio.setAudioPath('nui://my_resource/html/other_sounds/');

.setRandomPlaybackRate(min, max)

Enables random pitch variation for each playback. Useful for natural-sounding repeated effects.

ParamTypeDefaultDescription
minnumberMinimum playback rate (e.g. 0.7)
maxnumberMaximum playback rate (e.g. 1.5)

Returns undefined

js
audio.setRandomPlaybackRate(0.8, 1.2);
audio.playAudio('footstep.mp3'); // Slightly different pitch each time

.setPlaybackRate(rate)

Sets a fixed playback rate.

ParamTypeDefaultDescription
ratenumberThe playback rate (1.0 = normal speed)

Returns undefined

js
audio.setPlaybackRate(1.5); // 1.5x speed

.isPlaying()

Checks if audio is currently playing.

Returns booleantrue if playing.

js
if (audio.isPlaying()) {
    audio.stop();
}

.getDuration()

Returns the total duration of the current audio file.

Returns number|false — duration in seconds, or false if not available.

js
const duration = audio.getDuration();

.getCurrentTime()

Returns the current playback position.

Returns number — current time in seconds.

js
const time = audio.getCurrentTime();

.getDurationInPercent()

Returns the playback progress as a percentage.

Returns number|false — progress between 0 and 1, or false if duration is not available.

js
const progress = audio.getDurationInPercent(); // e.g. 0.42

.resetAudio()

Pauses playback and removes the audio element from the DOM.

Returns undefined

js
audio.resetAudio();

Static SFX Methods

Built-in UI sound effects. No instance needed — call directly on the class. Each accepts an optional volume parameter.

MethodDefault VolumeDescription
NssAudio.playSfxBack(volume)Back/cancel sound
NssAudio.playSfxNext(volume)0.5Next/confirm sound
NssAudio.playSfxUpDown(volume)0.25Navigation sound (hover)
NssAudio.playSfxShowMenu(volume)Menu open sound
NssAudio.playSfxIndexOpen(volume)Index open sound
NssAudio.playSfxIndexClose(volume)Index close sound
NssAudio.playSfxHideMenu1(volume)Menu close variant 1
NssAudio.playSfxHideMenu2(volume)Menu close variant 2
NssAudio.playSfxHideMenu3(volume)Menu close variant 3
js
NssAudio.playSfxBack(0.25);
NssAudio.playSfxShowMenu(0.5);

Master Volume

Control the global SFX volume with:

MethodDescription
NssAudio.setMasterVolumeInPercent(percent)Set master volume (0–100)
NssAudio.resetMasterVolumeInPercent()Reset to default
js
NssAudio.setMasterVolumeInPercent(50); // 50% of all SFX volume

Events / Callbacks

None. Use the Promise returned by playAudio() to react to playback completion.

CSS

None. NssAudio has no visual representation.

Dependencies

  • NssUiComponentInterface — base class (no NssResponsive dependency).

Things to Know

TIP

AbortError on play() is silently ignored (common when rapidly switching sounds). All other DOMException errors are logged to console.

INFO

Static SFX methods use an internal singleton player pointing to nui://nss_libs/ui/NssAudio/sfx/.

  • NssButtons, NssModal, and NssPadLock use NssAudio internally for their UI sounds.

NIGHTSHIFT STUDIO Documentation