如何使用纯JavaScript在按钮点击时切换暂停/播放音频?

huangapple go评论67阅读模式
英文:

How to toggle pause/play audio with button click in Pure Javascript?

问题

I find in Stackoverflow before but almost example working in

有没有办法在使用Javascript时切换单击播放/暂停?

HTML代码:

<button id="music" class="play-song"></button>

我的JS代码如下:

<script>
const elem = document.getElementById("music");
if (elem) {
elem.addEventListener ("click", playMusic, false);
}

function playMusic() {
  var musik= new Audio("/songs/1.mp3");
  musik.play();
  var current = this;
  if (current != elem) {
    elem.classList.remove('mute-audio');
  } else if (current.classList.contains('mute-audio') === true) {
    current.classList.remove('mute-audio');
  } else {
    current.classList.add('mute-audio');
  }
}
</script>
英文:

I find in Stackoverflow before but almost example working in <audio> element.

Have any method toggle click play/pause when using Javascript?

HTML Code:

&lt;button id=&quot;music&quot; class=&quot;play-song&quot;&gt;&lt;/button&gt;

My JS code look like:

&lt;script&gt;
const elem = document.getElementById(&quot;music&quot;);
if (elem) {
elem.addEventListener (&quot;click&quot;, playMusic, false);
}

function playMusic() {
  var musik= new Audio(&quot;/songs/1.mp3&quot;);
  musik.play();
  var current = this;
  if (current != elem) {
    elem.classList.remove(&#39;mute-audio&#39;);
  } else if (current.classList.contains(&#39;mute-audio&#39;) === true) {
    current.classList.remove(&#39;mute-audio&#39;);
  } else {
    current.classList.add(&#39;mute-audio&#39;);
  }
}
&lt;/script&gt;

答案1

得分: 1

以下是您要翻译的代码部分:

const elem = document.getElementById("music");
if (elem) {
  elem.addEventListener("click", playMusic, false);
}

var musik = new Audio("https://xomkey.com/wp-content/uploads/songs/xomkey-song.mp3");

function playMusic() {  
  if (musik.paused) {
    musik.play();
    // do your other task
  } else {
    musik.pause();
    // do your other task
  }
}
<button id="music" class="play-song">播放/暂停</button>

希望这对您有所帮助。如果您有其他需要,请随时告诉我。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const elem = document.getElementById(&quot;music&quot;);
if (elem) {
elem.addEventListener (&quot;click&quot;, playMusic, false);
}

var musik= new Audio(&quot;https://xomkey.com/wp-content/uploads/songs/xomkey-song.mp3&quot;);

function playMusic() {  
  if(musik.paused){
    musik.play();
    // do your other task
  } else {
    musik.pause();
    // do your other task
  }
  
}

<!-- language: lang-html -->

&lt;button id=&quot;music&quot; class=&quot;play-song&quot;&gt;Play/Pause&lt;/button&gt;

<!-- end snippet -->

You can play and pause with this piece of code like toggle. For audio, there are many more interesting methods.

For your reference:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement

https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement

huangapple
  • 本文由 发表于 2023年5月30日 01:17:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定