如何构建一个按钮来单击并播放/停止音频?
我想构建一个播放音频的图像按钮。 我的版本可以工作,但当我想在一个网站上多次使用它时,它只播放一个 mp3,而不播放其他 mp3。
我的代码:
<audio loop="false" src="audio_01.mp3"> </audio>
<p><img alt="" class="hover_pic" src="image.png" style="width: 40%;cursor:pointer" /></p>
<script>
var aud = document.getElementById("ASong").children[0];
var isPlaying = false;
aud.pause();
function playPause() {
if (isPlaying) {
aud.pause();
} else {
aud.play();
}
isPlaying = !isPlaying;
}
</script></div>
那么
<div id="BSong" onclick="playPause()" type="button">
<audio loop="false" src="audio_02.mp3"> </audio>
<p><img alt="" class="hover_pic" src="image.png" style="width: 40%;cursor:pointer" /></p>
<script>
var aud = document.getElementById("BSong").children[0];
var isPlaying = false;
aud.pause();
function playPause() {
if (isPlaying) {
aud.pause();
} else {
aud.play();
}
isPlaying = !isPlaying;
}
</script></div>
您知道该按钮仅在网站上播放其中之一的问题是什么吗?
i want to build a image button, that plays an audio.
My Version works but when I want to use it more than once on a site, it only play one mp3, not the other ones.
My Code:
<audio loop="false" src="audio_01.mp3"> </audio>
<p><img alt="" class="hover_pic" src="image.png" style="width: 40%;cursor:pointer" /></p>
<script>
var aud = document.getElementById("ASong").children[0];
var isPlaying = false;
aud.pause();
function playPause() {
if (isPlaying) {
aud.pause();
} else {
aud.play();
}
isPlaying = !isPlaying;
}
</script></div>
and
<div id="BSong" onclick="playPause()" type="button">
<audio loop="false" src="audio_02.mp3"> </audio>
<p><img alt="" class="hover_pic" src="image.png" style="width: 40%;cursor:pointer" /></p>
<script>
var aud = document.getElementById("BSong").children[0];
var isPlaying = false;
aud.pause();
function playPause() {
if (isPlaying) {
aud.pause();
} else {
aud.play();
}
isPlaying = !isPlaying;
}
</script></div>
So you have an idea what the problem is that the button only play one of them on the website?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。原文
发布评论
评论(2)
您多次使用相同的变量名称,例如 aud、isPlayig 等。
要解决此问题,您应该仅声明一次整个脚本并形成 onclick="playPause()" 发送您要播放的歌曲的 id 。
请注意是否已经有一些音乐正在播放。
You are using the same variable names multiple times like aud, isPlayig, etc..
To solve this issue, you should declare only once the whole script and form the onclick="playPause()" send the id of the song you want to play.
Be aware if there is already some music which is playing.
很难说出当前的两个代码片段是如何相对彼此排列的,但是每次您想要添加另一个轨道时一遍又一遍地复制代码将是难以维护的。就目前情况而言,
isPlaying
和aud
的变量可能会相互覆盖,具体取决于它们的布局方式,即使它们位于不同的脚本中。在脚本顶部使用const
或let
代替var
和use strict;
可以帮助检测这些别名。您可以在每个元素周围添加闭包以保持它们不同,但更好的方法是编写一个循环(也充当作用域闭包)并动态地将侦听器添加到每个元素。例如:
请注意,上面的代码允许同时播放多个音频文件。如果您想在单击新音频元素时停止所有其他音频元素并重置其时间,可以使用循环或额外变量来跟踪当前正在播放的曲目。例如:
关于您的代码的一些注释:
isPlaying
变量,因为audio
元素已经使用audioElement.paused
跟踪其播放/暂停状态。如果您在外部状态下跟踪它,如果您的变量和音频元素的状态不同步,则会增加进一步的复杂性和错误空间。放入
通常是
或
的子级(可能是
) code> 在本例中),在所有 HTML 标记关闭之后。
onclick
通常是不好的做法。 HTML 应该是结构性的,而不是行为性的。同样,style="width: 40%;cursor:pointer"
应移动到外部样式表并应用于类。.children[0];
是选择轨道中音频元素的一种脆弱方法。如果您最终重新排列 div 中的元素,则此代码很容易被破坏。document.querySelector("#BSong audio")
的重构更加精确和稳健,尽管使用类而不是 id 可以更轻松地实现动态性,因此您不必手动输入每个曲目。hover_pic
将是hover-pic
。It's hard to tell how your two current code snippets are arranged with respect to each other, but duplicating the code over and over every time you want to add another track is going to be unmaintainable. As it stands, the variables for
isPlaying
andaud
probably overwrite each other, depending on how they're laid out, even if they're in different scripts. Usingconst
orlet
instead ofvar
anduse strict;
at the top of your script can help detect these aliases.You could add closures around each one to keep them distinct, but a better approach is to write a loop (which also acts as a scoping closure) and dynamically add the listener to each element. For example:
Note that the above code lets multiple audio files play at once. If you want to stop all other audio elements when a new one is clicked and reset their time, you can do that with a loop or an extra variable that keeps track of the currently-playing track. For example:
A few remarks on your code:
isPlaying
variables sinceaudio
elements already track their playing/paused state withaudioElement.paused
. If you track it in external state, you add further complication and room for bugs if your variable and the the audio element's state go out of sync.<script>
in a<div>
.<script>
is usually a child of<body>
or<head>
(probably<body>
in this case), after all of the HTML tags are closed.onclick
on an HTML element is generally poor practice. HTML should be structural, not behavioral. Similarly,style="width: 40%;cursor:pointer"
should be moved to an external stylesheet and applied to a class..children[0];
is a brittle way to select the audio element in a track. If you wind up rearranging elements in the div, this code is liable to break.document.querySelector("#BSong audio")
is more precise and robust to refactors, although using classes instead of ids enables easier dynamism so you don't have to type each track out by hand.hover_pic
would behover-pic
.绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!