gpt4 book ai didi

Javascript 在没有音频崩溃的情况下播放其他音频

转载 作者:行者123 更新时间:2023-12-03 10:33:29 25 4
gpt4 key购买 nike

我正在尝试使用 Javascript 播放一个又一个音频。我从向量(int)和我创建的一些字符串中获取音频文件名。这是我的代码,但我无法使其工作。

var audio = new Audio();
function playAll(){
var folder_name = "synthex/";
var file_name = ".sentence";
var audio_file = ".wav";

for(i=0;i<18;++i)
{
var dir_file = folder_name + voicesClicks[i];
dir_file = dir_file + file_name;

if(voicesClicks[i] == null){
continue;
}
//var value = voicesClicks[i];

dir_file = dir_file + i;
dir_file = dir_file + audio_file;

console.log(dir_file);

audio.src = dir_file;
audio.play();

}
}

最佳答案

我发现您的代码的一个主要问题是,audio 的 src 在每次迭代中不断变化,因此即使 audio.src 值是有效的 wav文件,您的代码最终会播放最后一个...

我有类似的要求,我做到了......

function playAll() {
// CHANGE this list to as per your requirement.
var urls = ['http://upload.wikimedia.org/wikipedia/en/f/f9/Beatles_eleanor_rigby.ogg',
'http://upload.wikimedia.org/wikipedia/commons/5/5b/Ludwig_van_Beethoven_-_Symphonie_5_c-moll_-_1._Allegro_con_brio.ogg'
];
var idx = 0;
var aud = new Audio();
aud.src = urls[idx];
aud.addEventListener('ended', function() { // for automatically starting the next.
idx++;
if (idx === urls.length) idx = 0;
aud.src = urls[idx];
aud.play();
});
aud.play();
}

document.addEventListener("DOMContentLoaded", playAll); // you can change this to button.onclick handler.

编辑:您可以将url更改为...

var urls = [];
for(i=0;i<18;++i){
if(!voicesClicks[i]){
continue;
}
urls.push(folder_name + voicesClicks[i] + file_name + i + audio_file);
}
console.log(JSON.stringify(urls)); // for debug

关于Javascript 在没有音频崩溃的情况下播放其他音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29103003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com