收藏
回答

operateAudio:fail audioInstance is not set?

真机中播放语音失败 ,返回错误信息operateAudio:fail audioInstance is not set,开发者上可以,ios真机报错, 使用的是innerAudioContent 对象,而且仅可以播放一次,后续在调用play就不管用了。

最后一次编辑于  2021-10-27
回答关注问题邀请回答
收藏

3 个回答

  • 肖小涵
    肖小涵
    2021-10-27

    后来发现了问题呢,好像在真机上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
            }
    
        }
    }
    


    2021-10-27
    有用 1
    回复
  • Cjiang
    Cjiang
    2021-10-26

    你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html),并详细描述下复现流程。

    2021-10-26
    有用
    回复
  • 拾忆
    拾忆
    2021-10-26

    这个问题有一段时间了,目前应该还没解决方案。

    2021-10-26
    有用
    回复 1
    • 肖小涵
      肖小涵
      2021-10-27
      到现在 还是没有解决方案? 我擦
      2021-10-27
      回复
登录 后发表内容