Audio playback order of paragraphs

Problems encountered when developing, there are three sections of the audio, you need to play in a certain order. The word is the principle Audio listen object ended event, replace the trigger playing resources and play, Here is the code:

export const playWithOrder = (playArr) => {
    if(playArr.length === 0) return;
    // 创建Audio对象 设置对象属性
    const audio = new Audio();
    audio.preload = true; 
    audio.controls = true;
    audio.src = playArr.shift();
    // 开始播放第一个
    const promise = audio.play();
    promise.catch(err => {
        console.log('播放失败,请重试');
    })
    // 监听播放完成事件
    audio.onended = () => {
        if (playArr.length === 0) {
            return;
        }else {
            audio.src=playArr.shift();
            const promise = audio.play();
            promise.catch(err => {
                console.log('播放失败,请重试');
            })
        }
    }
}

This is based on the code to the project in addition to code business processing section, the test can not be used yet, but the overall idea has unfolded, I hope all of you to help O (∩_∩) O

Published 47 original articles · won praise 38 · views 60000 +

Guess you like

Origin blog.csdn.net/qq8241994/article/details/103243322