离子2如何发挥音效 [英] ionic 2 how to play sound effects

查看:346
本文介绍了离子2如何发挥音效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

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

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.

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

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.

非常感谢你的帮助。

推荐答案

安装:

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

用法:

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

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

这篇关于离子2如何发挥音效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆