class myAudio { constructor(arg) { // this.id = arg.id; // this.src = arg.src; this.fn = arg.fn || function(res) { // console.log(res) } //实例化声音 // let doit = () => { this.state = '1'; this.uni = arg.uni; this.Audio = this.uni.createInnerAudioContext(); // this.Audio.src = this.src; this.Audio.onPlay(() => { // console.log("开始播放", this.src); }) this.Audio.onEnded(() => { // console.log("音频自然播放结束事件", this.src); // doit(); this.state = '1'; }) this.Audio.onTimeUpdate((res) => { // console.log("音频播放进度更新事件", res, this.src); }) this.Audio.onError((res) => { // console.log(res.errMsg); // console.log(res.errCode); }) // } // doit(); } $play(src) { // console.log(this.state) if (this.state == '2') { //之前有播放 需要先清除 this.Audio.destroy(); this.Audio = this.uni.createInnerAudioContext(); } this.Audio.src = '/static/song/' + src + '.mp3'; this.Audio.play(); this.state = '2'; } $pause() { if (!this.Audio.paused) { this.Audio.pause(); this.state = '2'; } } $stop() { if (this.Audio) { this.Audio.stop(); this.state = '2'; } } $destroy() { if (this.Audio) { this.Audio.destroy(); this.state = '2'; } } } export default myAudio