后来发现了问题呢,好像在真机上innerAudioContent实例的onCanplay和 play()事件只会触发一次。所以第一次创建innerAudioContent对象后会播放一次,但是后续就不会触发了。 我的解决方案就是 每一次播放都 新创建一个innerAudioContent对象,并且取消就事件, 重新注册事件,这样就可以解决了!!!耶! 代码如下: class Audio { constructor() { console.log('创建播放对象', this.innerAudioContext) this.innerAudioContext = null } event() { let that = this if (!this.innerAudioContext) return; this.innerAudioContext.onError((err) => { console.log('audil_error', err) }) this.innerAudioContext.onCanplay(() => { console.log('播放对象unready') that.innerAudioContext.play() }) this.innerAudioContext.onEnded(() => { console.log('播放完毕') that.destroy() }) } init() { this.innerAudioContext = Taro.createInnerAudioContext() console.log('init创建播放对象', this.falseinnerAudioContext) this.event() } async play(sound) { let that = this if (!sound) { Utils.showInfo('未找到音频文件') return } if(this.innerAudioContext){ this.innerAudioContext.offEnded() this.innerAudioContext.onCanplay() this.innerAudioContext.onEnded() this.innerAudioContext.destroy() this.innerAudioContext = null } this.init() this.innerAudioContext.stop() let src = encodeURI(RESOURCE_HOST + sound) console.log('播放对象', src) this.innerAudioContext.src = src this.innerAudioContext.startTime = 0 this.innerAudioContext.loop = false } destroy() { console.log('销毁播放对象') if (this.innerAudioContext) { this.innerAudioContext.destroy() this.innerAudioContext = null } } }
operateAudio:fail audioInstance is not set?真机中播放语音失败 ,返回错误信息operateAudio:fail audioInstance is not set,开发者上可以,ios真机报错,[图片] 使用的是innerAudioContent 对象,而且仅可以播放一次,后续在调用play就不管用了。
2021-10-27