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

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

问题描述

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

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

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

用户可以通过测验来锻炼自己.我想在用户提交答案时播放音效.

两种不同的声音:一种代表好答案,另一种代表错误答案.

我已经尝试过使用 Nativeaudio、angular-audio 和 Ionic 音频模块,但每次文档都是基于 javascript 而不是 typescript,或者它没有帮助.

使用原生音频,我在播放背景声音后就成功了,但在它根本不起作用并出现错误之后:异常:未捕获(承诺):指定音频 id 的引用不存在.

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

非常感谢您的帮助.

解决方案

安装:

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

用法:

从'@ionic-native/native-audio'导入{NativeAudio};构造函数(私有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);//可以选择在文件播放完毕时传递要调用的回调this.nativeAudio.play('uniqueId1', () => console.log('uniqueId1 播放完毕'));

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

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.

解决方案

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/

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

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