JavaScript一个接一个地播放音频HTML5 [英] JavaScript play audio one after another HTML5

查看:131
本文介绍了JavaScript一个接一个地播放音频HTML5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个动态创建的javascript音频对象接连播放音频时遇到问题。音频来自谷歌通过一系列字符串。

  for(i = 0; i< Txt_array.length; i ++){
audio.src ='http ://translate.google.com/translate_tts&安培; TL = EN&安培; q ='+ Txt_array [I];
audio.play();};

我想通过事件侦听器来做到这一点,或者还有其他方法吗?它不只是谷歌,所以我想问你一个通用的解决方案,而不是现成的谷歌语音脚本。



你能帮助我吗?我怎样才能一个接一个地播放音频?



更新:

  audio.src ='http://translate.google.com/translate_tts?& tl = en& q ='+ Txt_array [0]; 
audio.play();

audio.addEventListener('ends',function(){
for(i = 1; i< Txt_array.length; i ++){
audio.src ='http ://translate.google.com/translate_tts?& tl = en& q ='+ Txt_array [i];
audio.play();};
},false);

我添加了Eventlistener,我猜它应该像这样工作,但我仍然有一个问题,如果这是第一个播放的文件,Eentlistener不会触发,对吗?



我很乐意提供进一步的支持。


解决方案

有一个 ends 事件,您可以更改侦听器中的 src 属性它。

  var strings =你好吗.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 ++;
}
};


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.

for (i = 0; i < Txt_array.length; i++) {
audio.src ='http://translate.google.com/translate_tts?&tl=en&q='+Txt_array[i];
audio.play();};

I would like to do this by an event listener, or is there any other way? It is not just about Google, so I would like to ask you for a general solution, not a ready made google speech script.

Could you help me? How can I play the audio one after another?

Update:

audio.src='http://translate.google.com/translate_tts?&tl=en&q=' + Txt_array[0];
audio.play();

audio.addEventListener('ended', function(){
for (i = 1; i < Txt_array.length; i++) {
audio.src ='http://translate.google.com/translate_tts?&tl=en&q='+Txt_array[i];
audio.play();};
}, false);

I added the Eventlistener, I guess it should work like this, but I still hae a problem that the Eentlistener does not fire if this is the first file played, right?

I would be happy for further support.

Thank you in advance.

解决方案

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++;
    }
};

这篇关于JavaScript一个接一个地播放音频HTML5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆