阅读视图

发现新文章,点击刷新页面。

记 华为鸿蒙机型小程序使用uni.createInnerAudioContext() 播放音频播放两次的问题

记 华为鸿蒙机型小程序使用uni.createInnerAudioContext() 播放音频播放两次的问题,被这个问题折磨了两天

直接上解决方案demo 代码 主要是红框代码是解决鸿蒙播放两次的问题,播放前先初始化一遍实例,然后赋值音频url,延迟300ms销毁在重新创建实例播放音频就好了

image.png

async play(item) {
                try {
                    const url = await this.getAudioSrc(item);
                    innerAudioContext = uni.createInnerAudioContext();
                    innerAudioContext.src = url;
                    setTimeout(() => {
                        this.pause();
                        innerAudioContext = uni.createInnerAudioContext();
                        innerAudioContext.src = url;
                        innerAudioContext.onPlay(() => {
                            console.log('开始播放');
                        });
                        innerAudioContext.onError((res) => {
                            console.log(res.errMsg);
                            console.log(res.errCode);
                        });
                        innerAudioContext.play();
                    }, 300);
                } catch (error) {
                    //TODO handle the exception
                }
            },
❌