gpt4 book ai didi

Jquery更改音频的src并在keydown上播放

转载 作者:行者123 更新时间:2023-12-03 00:34:37 25 4
gpt4 key购买 nike

除了案例 78(字母 N)之外,我有以下代码有效。按下此键时,音频开始播放,然后停止。其他键播放新的音频文件。我认为问题可能与更改 src 文件时按下的 keydown 有关。这是一个有效的 fiddle :keysounds

<!DOCTYPE html>

<html>
<head>
<script
type = "text/javascript"
src = "https://code.jquery.com/jquery-3.2.1.min.js"
integrity = "sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin = "anonymous">
</script>
<script>
/*global $ */
$(function() {
var playing;
$(document).on("keydown", function(key) {
playing = undefined;
switch(parseInt(key.which, 10)) {
case 68:
playing = $("#sound0").get(0);
break;
case 78:
playing = $("#sound0").get(0);
$("#sound0").attr("src",'sound2.mp3');
break;
case 79:
playing = $("#sound0").get(0);
break;
case 83:
playing = $("#sound0").get(0);
break;
case 85:
playing = $("#sound0").get(0);
break;
};

if(playing){
playing.play();
}

}).on("keyup", function() {
if(playing){
playing.pause();
playing.currentTime=0;
playing = undefined;
}
});
});
</script>
<style>
div{
text-align:center;
margin-top:50%;
}
span{
border-style:double;
margin:.4%;
}
</style>
<title>Keys</title>
</head>
<body>
<div>
<span>S</span>
<span>O</span>
<span>U</span>
<span>N</span>
<span>D</span>
<span>S</span>
</div>
<audio id="sound0" src="sound0.wav" ></audio>
</body>
</html>

最佳答案

您更改 src属性在获得 playing 之后多变的。

如果您只是反转两行,它应该可以工作,如下所示:

case 78:
$("#sound0").attr("src",'sound2.mp3');
playing = $("#sound0").get(0);
break;

您还应该为下一次迭代重置原始音频源。

编辑

使用“标志”来确定 key 是否被持有避免了 keydown事件太频繁地触发你的脚本......并让音乐播放。
/*global $ */
$(function() {
var playing;
var keyHeld=false; // Flag!

$(document).on("keydown", function(key) {
if(!keyHeld){
keyHeld=true;

// Reset to original sound
$("#sound0").attr("src",'http://www.sailwbob.com/sound0.wav');


switch(parseInt(key.which, 10)) {
case 68:
playing = $("#sound0").get(0);
break;
case 78:
$("#sound0").attr("src",'http://www.sailwbob.com/sound2.mp3');
playing = $("#sound0").get(0);
break;
case 79:
playing = $("#sound0").get(0);
break;
case 83:
playing = $("#sound0").get(0);
break;
case 85:
playing = $("#sound0").get(0);
break;
default:
$("#sound0").attr("src",''); // On other key, Play nothing!
playing = $("#sound0").get(0);
};

if($("#sound0").attr("src")!=""){
playing.play();
}
}
}).on("keyup", function() {
if(playing){
playing.pause();
playing.currentTime=0;
keyHeld=false;
}
});
});

这是您的 Fiddle updated

关于Jquery更改音频的src并在keydown上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43104474/

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