gpt4 book ai didi

jquery - 音频暂停/播放

转载 作者:行者123 更新时间:2023-12-02 23:11:21 29 4
gpt4 key购买 nike

我不知道我在做什么错

<script>
$(document).ready(function() {
var playing = false;
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'star.mp3');
$('#star').click(function() {
playing == true;
if(playing == true) audioElement.pause();
else audioElement.play();
});
});

它的意思是,当播放一种声音而我单击另一种声音时,已经播放的声音应该停止,而另一种声音应该开始。

最佳答案

我认为您的问题在这里:

$('#star').click(function() {
playing == true; // should be = not == if you're trying to assign
if(playing == true) audioElement.pause(); // but if you assign then playing is always true
else audioElement.play();
});

参见 assignment operators
comparison operators

这是一个简化的版本,可能会有所帮助:

$(document).ready(function() {
var audio1 = $("#DunDunDun")[0];
var audio2 = $("#WaWaWaa")[0];
$("#ominousMusic").click(function() {
audio1.play();
audio2.pause();
});
$("#sadMusic").click(function() {
audio2.play();
audio1.pause()
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ominousMusic" title="go on, click it, you know you want to...">
cue ominous music...
</div>
<audio id="DunDunDun">
<source src="http://pluggedinwebdesign.com/media/DunDunDun.mp3">
Your browser isn't invited to super fun audio time.
</audio>
<div id="sadMusic" title="go on, click it, you know you want to...">
cue sad music...
</div>
<audio id="WaWaWaa">
<source src="http://pluggedinwebdesign.com/media/Sad_Trombone.mp3">
Your browser isn't invited to super fun audio time.
</audio>

关于jquery - 音频暂停/播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415179/

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