如何使声音文件在多个重叠的实例中播放
How do you make a sound file play in multiple overlapping instances?
我正试图获得一个声音文件播放速度更快,以跟上下面代码中的文本显示。声音工作(无论如何在Firefox中),但它似乎一次只能播放文件的一个实例。
我想让每个字母弹出屏幕并伴随着弹出的声音。现在弹出的声音是随机的,并不是定时播放每个字母。
我想知道我是否需要有声音对象的多个实例,以及如何做到这一点。
我已经尽可能地缩短了声音文件,并且文件的长度比我使用的setTimeout
间隔短。它只是不会重叠相同声音文件的多个副本,出于一些非常好的原因,我不知道,我敢肯定。
完整代码如下:
(我试过JSFiddle,但不能让它工作(我将保存这个问题为以后的日期))
<html>
<head>
<style>
#display {
color: white;
font-size: 150%;
padding: 2em 5em;
min-height: 600px;
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
body {
background-color: black;
padding: 0;
margin:0;
}
</style>
<script>
var text = "Test String... 1, 2, 3; Everything seems to be working, but the sound is lagging.";
var charDelay = 40; // Sets the delay time for the character loop
function loadText() {
var i = 0; // Character counter
var myPud = new Audio("http://southernsolutions.us/audio/pud03.ogg");
var displayBox = document.getElementById("display");
displayBox.innerHTML = "<p>";
textLoop();
function textLoop() {
if (i == text.length){ // This condition terminates the loop
displayBox.innerHTML += "</p>";
return;
} else if (i < text.length) { // This condition appends the next character
displayBox.innerHTML += text[i];
i++;
myPud.play();
setTimeout(function(){return textLoop()}, charDelay);
}
}
}
window.onload = function(){loadText()};
</script>
</head>
<body>
<div id="display"></div>
</body>
</html>
我建议你让声音循环更长一点,比如1秒。然后,通过事件侦听器控制声音播放,这样一旦文本完成,您就可以停止声音播放。
试着像你现在做的那样做,你可以加快声音在音频文件中的播放速度。这应该会产生更好的结果。
下面是我测试过的一些代码,可以让您通过事件侦听器来实现。结果与你所拥有的相似,但如果你将音频文件增加到1秒,并将其更改为24次点击,你就会得到你想要的确切效果。
编辑:考虑到评论,我也更新了下面的内容。
<script>
var text = "Test String... 1, 2, 3; Everything seems to be working, but the sound is lagging.";
var charDelay = 40; // Sets the delay time for the character loop
function loadText() {
var i = 0; // Character counter
var myPud = new Audio("http://southernsolutions.us/audio/pud03.ogg");
var displayBox = document.getElementById("display");
// Toggle for whether to loop
var stillPlay = true;
displayBox.innerHTML = "<p>";
// Listen for when it ends
myPud.addEventListener("ended", onAudioComplete);
// Begin playing
myPud.play();
// Start the loop
textLoop();
function textLoop() {
if (i == text.length){ // This condition terminates the loop
displayBox.innerHTML += "</p>";
// If we're at the end, we want to stop playing
stillPlay = false;
// Rather than duplicate code, jump straight into the complete function
onAudioComplete(null);
return;
} else if (i < text.length) { // This condition appends the next character
displayBox.innerHTML += text[i];
i++;
// Direct reference to the function to avoid more anony. functions
setTimeout(textLoop, charDelay);
}
}
// On audio complete
function onAudioComplete(e){
// Can we still play? If so, play
if(stillPlay){
myPud.play();
} else {
// Otherwise, remove the event listener, stop and null out.
myPud.removeEventListener("ended", onAudioComplete);
myPud.stop();
myPud = null;
}
}
}
window.onload = loadText;
</script>
相关文章:
- ES6构造函数返回基类的实例
- KnockoutJS-组件-多个实例
- 为什么无法在TypeScript中导出类实例
- 如何在GoogleWeb工具包(GWT)中从JSNI调用接口(实例化)
- fluxxor向一个flux实例添加一组以上的操作
- 使用jQuery获取Dropzone实例/对象
- HighCharts长标题文本在某些元素上重叠
- "实例范围”;TypeScript类的getter/setter
- 如何在速度模板中获取LiferayPortlet实例id
- 同一项怎么可能在一个实例中未定义,却在另一个实例上定义
- 如何检测重叠元素下的单击
- 实例创建(JS)
- 显示模块模式在Knockout中设置模型的新实例
- 如何获取单选按钮的多个实例的选定单选按钮值
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- 在哪里可以找到RXUI Javascript'时间飞逝'实例
- spine.js在启动时填充模型实例
- 不再需要时使用jQuery/kill实例
- 谷歌地图重叠MarkerSpiderfier实例化问题
- 如何使声音文件在多个重叠的实例中播放