如何使用不同的SRC切换多个播放/暂停按钮

发布于 2025-01-25 07:43:49 字数 918 浏览 4 评论 0原文

我的游戏/暂停按钮正在面临一些问题。在YM HTML中,我有多个具有不同来源的按钮:

<div>
 <button class="button-53" id="button">Play</button>
</div>

<audio id="player">
  <source src='audio/mixdown.mp3' type='audio/mpeg'/>
</audio>

<div>
 <button class="button-53" id="button">Play</button>
</div>

<audio id="player">
  <source src='audio/mixdown2.mp3' type='audio/mpeg'/>
</audio>

这是我的JavaScript:

var buttons = document.getElementById("button");
var music = document.getElementById("player");

for (const buttons of button) {
buttons.addEventListener("click", function(){
  if(music.paused){
    music.play();
    buttons.innerHTML = "Pause";
  } else {
    music.pause();
    buttons.innerHTML = "Play";
  }

});
}

但是,当我从第一个按钮播放音乐然后暂停它时,请转到下一个按钮并切换按钮,声音从我的位置开始播放。停止它,而不是从书面源播放声音。

这里有人可以帮助我吗?

I'm facing some issues with my play/pause buttons. In ym HTMl I have multiple buttons with different sources:

<div>
 <button class="button-53" id="button">Play</button>
</div>

<audio id="player">
  <source src='audio/mixdown.mp3' type='audio/mpeg'/>
</audio>

<div>
 <button class="button-53" id="button">Play</button>
</div>

<audio id="player">
  <source src='audio/mixdown2.mp3' type='audio/mpeg'/>
</audio>

This is my Javascript:

var buttons = document.getElementById("button");
var music = document.getElementById("player");

for (const buttons of button) {
buttons.addEventListener("click", function(){
  if(music.paused){
    music.play();
    buttons.innerHTML = "Pause";
  } else {
    music.pause();
    buttons.innerHTML = "Play";
  }

});
}

But when I play music from the first button and then pause it, go over to the next button and toggle the button, the sound keeps playing from that point where I stopped it instead of playing the sound from the written source.

Anyone here that could help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2025-02-01 07:43:49

HTML中的ID属性应该是唯一的。 document.getElementById返回一个元素(如果找到)。

解决问题的一种解决方案是给出每对按钮/音频元素不同的ID,例如

<div>
 <button class="button-53" id="button1">Play</button>
</div>

<audio id="player1">
  <source src='audio/mixdown.mp3' type='audio/mpeg'/>
</audio>

<div>
 <button class="button-53" id="button2">Play</button>
</div>

<audio id="player2">
  <source src='audio/mixdown2.mp3' type='audio/mpeg'/>
</audio>

,为每对添加事件侦听器:

function addListeners(buttonElement, audioElement) {
    // ...add implementation
}

addListeners(
    document.getElementById("button1"),
    document.getElementById("player1")
);
addListeners(
    document.getElementById("button2"),
    document.getElementById("player2")
);

Id attributes in HTML should be unique. The document.getElementById returns a single element (if found).

One solution to fixing your problem is to give each pair of button/audio elements different ids, for example:

<div>
 <button class="button-53" id="button1">Play</button>
</div>

<audio id="player1">
  <source src='audio/mixdown.mp3' type='audio/mpeg'/>
</audio>

<div>
 <button class="button-53" id="button2">Play</button>
</div>

<audio id="player2">
  <source src='audio/mixdown2.mp3' type='audio/mpeg'/>
</audio>

and then add event listeners for each pair:

function addListeners(buttonElement, audioElement) {
    // ...add implementation
}

addListeners(
    document.getElementById("button1"),
    document.getElementById("player1")
);
addListeners(
    document.getElementById("button2"),
    document.getElementById("player2")
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文