Coder Social home page Coder Social logo

nativescript-sound's Introduction

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();

nativescript-sound's People

Contributors

jbristowe avatar

Stargazers

HolySeat avatar Sebastian Denis avatar Alessandro Santamaria avatar Ehab Ashour avatar Pezhvak avatar  avatar AlexanderT avatar Brad Martin avatar Shawn Makinson avatar Patrick Seda avatar Nathan Walker avatar TJ VanToll avatar

Watchers

 avatar Raef Akehurst avatar

nativescript-sound's Issues

Can't resolve 'nativescript-sound' - Nativescript 8.1.5

Hi, i updated nativescript to the last version (8.1.5).

I'm using nativescript-angular, and since then i can't use nativescript-sound or rather nativescript-sound-kak ( @methompson 's plugin ).

i have or

ERROR in ./src/app/scan/scan.component.ts 210:12-41
Module not found: Error: Can't resolve 'nativescript-sound' in 'C:\Users\krief\OneDrive\Documents\Projets\GitHub\visualy\VisualyScan\src\app\scan'
@ ./src/app/app-routing.module.ts 5:0-58 18:31-44
@ ./src/app/app.module.ts 2:0-56 38:12-28
@ ./src/main.ts 6:0-45 8:69-78

or

ERROR in ./node_modules/nativescript-sound-kak/sound-common.js 1:0-51
Module not found: Error: Can't resolve 'tns-core-modules/file-system' in 'C:\Users\krief\OneDrive\Documents\Projets\GitHub\visualy\VisualyScan\node_modules\nativescript-sound-kak'
@ ./node_modules/nativescript-sound-kak/sound.android.js 1:0-41 3:20-31
@ ./src/app/scan/scan.component.ts 210:12-45
@ ./src/app/app-routing.module.ts 5:0-58 18:31-44
@ ./src/app/app.module.ts 2:0-56 38:12-28
@ ./src/main.ts 6:0-45 8:69-78

package.json:

{
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "repository": "<fill-your-repository-here>",
  "dependencies": {
    "@angular/animations": "^12.2.5",
    "@angular/common": "^12.2.5",
    "@angular/compiler": "^12.2.5",
    "@angular/core": "^12.2.5",
    "@angular/forms": "^12.2.5",
    "@angular/http": "8.0.0-beta.10",
    "@angular/platform-browser": "^12.2.5",
    "@angular/platform-browser-dynamic": "^12.2.5",
    "@angular/router": "^12.2.5",
    "@nativescript/angular": "^12.2.0",
    "@nativescript/background-http": "~5.0.0",
    "@nativescript/camera": "~5.0.0",
    "@nativescript/core": "~8.1.1",
    "@nativescript/datetimepicker": "^2.1.9",
    "@nativescript/firebase": "^11.1.3",
    "@nativescript/geolocation": "~8.0.0",
    "@nativescript/imagepicker": "~1.0.0",
    "@nativescript/theme": "^3.0.1",
    "@ngx-translate/core": "~14.0.0",
    "@ngx-translate/http-loader": "~7.0.0",
    "@nstudio/nativescript-camera-plus": "^4.1.0",
    "@nstudio/nativescript-loading-indicator": "~4.0.0",
    "@nstudio/nativescript-pulltorefresh": "~3.0.0",
    "@nstudio/nativescript-snackbar": "~2.0.0",
    "nativescript-angular": "^10.0.0",
    "nativescript-imagecropper": "~4.0.0",
    "nativescript-nfc": "~5.0.0",
    "nativescript-permissions": "^1.3.8",
    "nativescript-sound": "^1.0.4",
    "nativescript-theme-core": "~1.0.4",
    "nativescript-ui-chart": "~9.0.0",
    "nativescript-ui-sidedrawer": "~10.0.0",
    "nativescript-webview-interface": "^1.4.2",
    "reflect-metadata": "~0.1.10",
    "rxjs": "~7.3.0",
    "tns-platform-declarations": "^6.5.15",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^12.2.5",
    "@nativescript/android": "8.1.1",
    "@nativescript/schematics": "~0.5.0",
    "@nativescript/types": "~8.1.0",
    "@nativescript/webpack": "rc",
    "@ngtools/webpack": "^12.2.5",
    "@types/jasmine": "^3.5.11",
    "typescript": "~4.3.5"
  },
  "readme": "NativeScript Application",
  "main": "./src/main.ts"
}

If someone can help thank you

Nativescript-sound crashes on iOS 13.2

When instantiating a new Sound object, Nativescript crashes with an error along the lines of 'null is not on object' evaluating this._player.initWithContentsOfURLError(this._url);

It turns out that new AVAudioPlayer() returns null. I updated the code to run AVAudioPlayer.alloc().initWithContentsOfURLError(this._url) and it worked.

I've made a Pull Request from a fork to fix this issue.

Volume detection

Hi,

This library is really useful. I'm using it. But now I'm stuck with a problem. I'm trying to detect user device volume level. My idea is to show and alert message if volume level is the lowest in user device, saying something like: 'hey dude, volume up your device please'.
I think the play() function of the library could detect it automatically. Are you planning on do something to detect volume level in devices? Usability will be highly improve.

Thanks,
Dani

Sound not initialized; file not found

in my app directory, I have a sound directory. inside that directory, I put audio.wav

I tried loading it using these

var tada = sound.create("~/sound/fail.wav");

var tada = sound.create("./sound/fail.wav");

var tada = sound.create("../sound/fail.wav");

var tada = sound.create("sound/fail.wav");

var tada = sound.create("/sound/fail.wav");

all gave me the same error "Sound not initialized; file not found."

I know this is related to my (lack of) understanding of nativescript, but just wondering if you know what I am doing wrong? any help would be greatly appreciated

stop() is not stopping the sounds from playing

The library works great for playing sounds, and is really simple to use, but I have 1 small issue with it. The .stop() command does not seem to work (on Android at least, not tested on iOS)

Given the following code the 5 second alarm sound plays to completion every time:

this.alarm = sound.create("~/sounds/alarm.mp3");
this.alarm.play();
setTimeout(() => { this.alarm.stop() }, 1000);

(Not my actual code... just a simple sample to illustrate the problem)

Any idea why?

Extending using a Podfile trouble

I really love this plugin! Nice work.
I'm trying to extend to support recording amongst other things however I'm running into some trouble and curious if you could provide a pointer or 2.

I've been experimenting here:
https://github.com/NathanWalker/sound-tester

I'm trying to include the EZAudio Pod (https://github.com/syedhali/EZAudio).
This issue props up everytime:

Check dependencies
build-debug.xcconfig line 2: Unable to find included file "Pods/Target Support Files/Pods/Pods.debug.xcconfig"
build-debug.xcconfig line 3: Unable to find included file "../plugins-debug.xcconfig"
CodeSign error: entitlements are required for product type 'Application' in SDK 'Simulator - iOS 9.2'. Your Xcode installation may be damaged.

I have opened the Xcode project and added a valid team provisioning profile. But the issue stems from something I'm missing in how to integrate the Podfile.
I'm looking forward to contribute some nice things here if I can get past this road block.

sound is not played.

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

image

No errors in console ( and if I put other non-existing filename - then it shows an error : "file not found") - so it does see the file.

Why doesn't it play ?

manifest

tnsinfo :
image

EDIT

If I put

  let tada = sound.create("~/raw/beep.mp3");

as a field ( outside a function):

tada = sound.create("~/raw/beep.mp3");

then it does work.

I don't want to initialize it as a field.

is it possible to use it as a psuedo : Promise(loadFile).then(playFile) ?

Audio is not played on NativeScript 1.5

It seems that the plugin does not work with NativeScript 1.5. I put the "sounds" folder with the file on "app" folder, then used this code:

var sound = require("nativescript-sound");
tap = sound.create("~/sounds/tap.mp3");
tap.play();

Its is not playing the sound!

Doesn't play some sounds

I have loaded 26 sounds but generally only plays the first 14 sounds. Some timesplay 16 first.
const sound = require("nativescript-sound"); const sounds = { "A": sound.create("~/assets/sounds/A.mp3"), "B": sound.create("~/assets/sounds/B.mp3"), "C": sound.create("~/assets/sounds/C.mp3"), "D": sound.create("~/assets/sounds/D.mp3"), "E": sound.create("~/assets/sounds/E.mp3"), "F": sound.create("~/assets/sounds/F.mp3"), "G": sound.create("~/assets/sounds/G.mp3"), "H": sound.create("~/assets/sounds/H.mp3"), "I": sound.create("~/assets/sounds/I.mp3"), "J": sound.create("~/assets/sounds/J.mp3"), "K": sound.create("~/assets/sounds/K.mp3"), "L": sound.create("~/assets/sounds/L.mp3"), "M": sound.create("~/assets/sounds/M.mp3"), "N": sound.create("~/assets/sounds/N.mp3"), "O": sound.create("~/assets/sounds/O.mp3"), "P": sound.create("~/assets/sounds/P.mp3"), "Q": sound.create("~/assets/sounds/Q.mp3"), "R": sound.create("~/assets/sounds/R.mp3"), "S": sound.create("~/assets/sounds/S.mp3"), "T": sound.create("~/assets/sounds/T.mp3"), "U": sound.create("~/assets/sounds/U.mp3"), "V": sound.create("~/assets/sounds/V.mp3"), "W": sound.create("~/assets/sounds/W.mp3"), "X": sound.create("~/assets/sounds/X.mp3"), "Y": sound.create("~/assets/sounds/Y.mp3"), "Z": sound.create("~/assets/sounds/Z.mp3") } methods:{ sonar:function(letra){ sounds[letra].play(); }, // End sonar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.