离子2如何发挥音效

Gui*_*hem 3 audio ionic2

我实际上正在使用Ionic 2/angular2开发应用程序.

这是一个学习使用SQLite数据库运行的英语时态的应用程序.

我想添加一个循环播放的背景声音.

用户可以通过quizz来锻炼身体.我想在用户提交答案时播放声音效果.

两种不同的声音:一种是好的,一种是坏的答案.

我已经尝试过使用Nativeaudio,角度音频和Ionic音频模块,但每次文档都是基于javascript而不是打字稿,或者它没有帮助.

使用原生音频,我在播放背景音后取得了成功,但在它完全不起作用后出现了错误:EXCEPTION:Uncaught(in promise):指定的音频ID不存在引用.

对于其他解决方案(角度音频和离子音频)要么我没有得到如何安装它,一旦安装,我什么都没有:没有声音,没有错误.

非常感谢您的帮助.

小智 8

安装:

$ ionic plugin add --save cordova-plugin-nativeaudio
$ npm install --save @ionic-native/native-audio
Run Code Online (Sandbox Code Playgroud)

用法:

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'));
Run Code Online (Sandbox Code Playgroud)

参考:https: //ionicframework.com/docs/native/native-audio/