nativescript-sound
by John Bristowe | v1.0.4
Play a sound in your NativeScript app
npm i --save nativescript-sound
  • Version: 1.0.4
  • GitHub:
  • Downloads:
    • Last Day:   0
    • Last Week:  0
    • Last Month: 0

NativeScript Sound

Play a sound in your NativeScript app.

Installation

Run the following command from the root of your project:

tns plugin add nativescript-sound

Usage

To use this plugin you must first require() it:

var sound = require("nativescript-sound");

create and play

It's important to preload the audio file into the sound module before playing it; there is a delay during creation due to the audio being processed:

var tada = sound.create("~/sounds/tada.mp3"); // preload the audio file

// play the sound (i.e. tap event handler)
tada.play();

A good way to do this is to create a sound collection:

sounds = {
"Tada": sound.create("~/sounds/tada.mp3"),
"Boo": sound.create("~/sounds/boo.mp3"),
// ...
};

If you wish to play a sound due to a button being tapped, leverage the following code (where name refers to the name of the audio file to be played):

this.playButtonPressed = function(name) {
if (app.android) {
sounds[name].play();
} else {
var soundFile = sound.create("~/sounds/" + name + ".mp3");
soundFile.play();
}
}

stop

tada.stop();

reset

tada.reset();