gpt4 book ai didi

javascript - 按顺序播放多个声音文件 仅播放最后一个声音

转载 作者:行者123 更新时间:2023-12-01 03:22:05 25 4
gpt4 key购买 nike

这个应用程序根据数字播放声音。我有多个音频文件,都是非常短的 mp3。正如问题所说,我希望它按顺序播放所有声音,一个接一个,但只有最后一个声音(数字)正在播放,我在控制台上收到错误消息:

“未捕获( promise 中)DOMException:play() 请求被新的加载请求中断。”

我遗漏了一些东西,或者也许这是不可能做到的。如有任何帮助,我们将不胜感激。

function playSound(note){

var currentPlayer;
var player = document.getElementById("player");

var isPlaying = player.currentTime > 0 && !player.paused && !player.ended
&& player.readyState > 2;


if (!isPlaying){

player.src = "sounds/"+note+".mp3";
player.play();

}else{
player.pause();
player.currentTime = 0;
currentPlayer = player;

}



}


//variable with numbers where each number should load a sound and play
var numString = "0934590042529689108538569377239609480456034083552";


for(i = 0; i < numString.length; i++){


switch (parseInt(numString[i])){
case 1:
playSound("C");
break;
case 2:
playSound("D");
break;
case 3:
playSound("E");
break;
case 4:
playSound("F");
break;
case 5:
playSound("G");
break;

case 6:
playSound("A");
break;

case 7:
playSound("B");
break;

case 8:
playSound("C2");
break;

case 9:
playSound("D2");
break;


case 0:
playSound("silence");
break;


}

HTML:

<audio controls id="player" style="display: none">
<source src="#"></source>
</audio>

最佳答案

您必须等待第一个注释完成才能加载下一个注释:

var index = 0;
var numString = "0934590042529689108538569377239609480456034083552";
var notes = ['silence', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'C2', 'D2'];
var player = document.getElementById('player');

function playNote() {
if (index >= numString.length) {
stop();
return;
}
var note = notes[Number(numString[index])]; // transform the number to the corresponding note ('1' => 'C')
if (!note) {
stop();
return;
}
index++; // when 'playNote' is called the next time, the next note will be played
player.src = `sounds/${note}.mp3`;
player.play(); // when this ends, the 'ended' event will be fired and 'playNote' will be called
}

function stop () {
player.removeEventListener('ended', playNote); // the last note has been played, remove the event listener
}

player.addEventListener('ended', playNote); // whenever the sound ends, call 'playNote'
playNote(); // start to play the first note

编辑:

我在 playNote 函数中将 this 更改为 player。当第一次调用此函数 (playNote()) 时,没有 this 对象引用 player。它应该是playNote.call(player)。但就目前情况而言,也应该如此。

要减少笔记之间的加载时间,您有两种可能性:

在多个audio中分别加载声音文件

对于每个音符,创建一个 new Audio() 并加载声音文件:

var numString = "0934590042529689108538569377239609480456034083552";
var notes = ['silence', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'C2', 'D2'];
var audios = {};
notes.forEach(note => {
var audio = new Audio();
audio.src = `sounds/${note}.mp3`; // load the sound file
audios[note] = audio;
});

var currentAudio = null; // the audio that is currently playing

function playNote () {
if (currentAudio) {
currentAudio.removeEventListener('ended', playNote); // remove the event listener from the audio that has just stopped playing
}
if (index >= numString.length) {
return;
}
var note = notes[Number(numString[index])]; // transform the number to the corresponding note ('1' => 'C')
if (!note) {
return;
}
currentAudio = audios[note];
index++; // when 'playNote' is called the next time, the next note will be played
currentAudio.play(); // when this ends, the 'ended' event will be fired and 'playNote' will be called
currentAudio.addEventListener('ended', playNote);
}

playNote();

使用AudioContext API

新的 Web Audio API 比简单的 new Audio() 复杂得多,但功能也更强大。您不需要在服务器上拥有所有可能的声音文件 - 您可以使用客户端的声音芯片来创建您想要的任何声音。

关于javascript - 按顺序播放多个声音文件 仅播放最后一个声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45127670/

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