JavaScript play audio one after another HTML5

前端 未结 1 2020
孤街浪徒
孤街浪徒 2020-12-10 21:09

I have a problem with playing audio from a dynamically created javascript audio object one after another. The Audio comes from Google over an array of strings.

<         


        
相关标签:
1条回答
  • 2020-12-10 21:29

    There is an ended event, you can change the src property in a listener for it.

    var strings = "Hello how are you".split(" ");
    var index = 1;
    
    audio.src='http://translate.google.com/translate_tts?&tl=en&q=' + strings[0];
    audio.play();
    
    audio.onended = function() {
        if(index < strings.length){
            audio.src='http://translate.google.com/translate_tts?&tl=en&q=' + strings[index];
            audio.play();
            index++;
        }
    };
    
    0 讨论(0)
提交回复
热议问题