3

I'm actually developping an application with Ionic 2 / angular2.

It's an app to learn english tenses that runs with a SQLite database.

I would like to add a background sound that plays in loop all the time.

The users can exercice themselves with a quizz. I would like to play sound effects when the user submit his answer.

Two different sounds : one for good and one for bad answers.

I've already tried with Nativeaudio, angular-audio and Ionic audio modules but each times the documentation is based on javascript and not typescript, or it is not helpfull.

With native audio, I've succeed once playing the background sound but after it didn't work at all and came up with an error : EXCEPTION: Uncaught (in promise): A reference does not exist for the specified audio id.

For the other solutions (angular-audio and ionic-audio) either i didn't get how to install it either, once installed, I had nothing : no sound and no error.

Thank you very much for your help.

Guilhem
  • 31
  • 1
  • 2
  • Hi and welcome to StackOverflow! Please take the tour on how to ask: https://stackoverflow.com/help/how-to-ask If you can improve your question, it will improve the odds of someone being able to help you! – Philipp Apr 07 '17 at 13:47
  • Did you check this link? http://ionicframework.com/docs/native/native-audio/ – Ehtesham Hasan Apr 13 '17 at 07:36
  • Hi, yes I did check the ionic documentation for native audio but couldn't find what I was looking for. – Guilhem Apr 14 '17 at 11:17

1 Answers1

8

Install:

$ ionic plugin add --save cordova-plugin-nativeaudio
$ npm install --save @ionic-native/native-audio

Usage:

import { NativeAudio } from '@ionic-native/native-audio';

constructor(private nativeAudio: NativeAudio) { }

...

this.nativeAudio.preloadSimple('uniqueId1', 'path/to/file.mp3').then(onSuccess, onError);
this.nativeAudio.preloadComplex('uniqueId2', 'path/to/file2.mp3', 1, 1, 0).then(onSuccess, onError);

this.nativeAudio.play('uniqueId1').then(onSuccess, onError);

// can optionally pass a callback to be called when the file is done playing
this.nativeAudio.play('uniqueId1', () => console.log('uniqueId1 is done playing'));

Reference: https://ionicframework.com/docs/native/native-audio/

Thewads
  • 4,953
  • 11
  • 55
  • 71
  • Thank you for your answer, for now i've fixed the problem simply using html5 audio tag. – Guilhem May 15 '17 at 10:37
  • Didn't work for me. The function I have created to play sound. Also added nativeAudio in the constructor. barCodeSound() { this.nativeAudio.preloadSimple('beep', '../assets/audio/beep.mp3'); this.nativeAudio.play('beep', function () { console.log('==============beep played============'); }); } Anything I am missing ? – Lalit Dashora Oct 25 '18 at 06:05