NativeScript Sound
Play a sound in your NativeScript app.
This project was originally programmed by John Bristowe. However when this plugin is used in a project installed on a phone with iOS 13.2, the entire application would crash. This fork is a modified version of the original project that solves the instantiation crash issue that I encountered. The rest of the project remained intact.
Installation
Run the following command from the root of your project:
npm i nativescript-sound-kak
Usage
To use this plugin you must first require or import it:
//CommonJsconst Sound = ; //ES6 Import;
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:
const beep = Sound; // preload the audio file // play the sound (i.e. tap event handler)beep;
You may wish to check that the file actually exists:
;; // currentApp().path leads to your app folder in the projectconst pathToBeep = fspath;let beep;if fsFile beep = Sound;
stop
beep;
reset
beep;
Background Playback
In iOS, the default playback method will silence all background sounds. You can define whether the audio playback in the app silences background audio (i.e. the Music app) or if it play concurrently.
;// Sets the audio playback to background, i.e. allows it to play at the same time as other background audio.Sound;
;// Turns off background playback. When the Sound object is created, background audio will be silencedSound;