参考地址:https://ionicframework.com/docs/v3/native/vibration/
https://ionicframework.com/docs/v3/native/native-audio/
震动的依赖
$ ionic cordova plugin add cordova-plugin-vibration
$ npm install --save @ionic-native/vibration@4
import { Vibration } from ‘@ionic-native/vibration‘;
?
constructor(private vibration: Vibration) { }
?
原生音频
$ ionic cordova plugin add cordova-plugin-nativeaudio
$ npm install --save @ionic-native/native-audio@4
import { NativeAudio } from ‘@ionic-native/native-audio‘;
?
constructor(private nativeAudio: NativeAudio) { }
?
//找一个声音文件,然后在ts文件里面先加载声音
constructor(private nativeAudio: NativeAudio) {
//uniqueId1为音频文件的唯一ID
//assetPath音频资产的相对路径或绝对URL(包括http://)
//官网还有更多的配置,这里只需要两个参数就行了,后面的回调记得带上
this.nativeAudio.preloadSimple(‘uniqueId1‘, ‘assets/13785.mp3‘).then(this.onSuccess, this.onError);
}
?
sound(){
//震动的时间
this.vibration.vibrate(1000);
//调用加入本地的音频
this.nativeAudio.play(‘uniqueId1‘).then(this.onSuccess,this.onError)
}