鼠标单击时暂停声音

时间:2018-05-18 16:39:23

标签: javascript audio soundplayer

我正在尝试设计自己的音频播放器。 我制作了“播放”,“暂停”和“停止”符号(Png格式),当您点击它们时,我找不到实际广告播放暂停和停止功能的解决方案。

我想出了点击“播放”一词时的声音播放方式(没有按钮)但是在添加“暂停”或“停止”时我迷失了。

我是一个使用html / css的菜鸟,所以如果我不是很清楚,请原谅我。 这就是我到目前为止所做的:

<audio id="audio" src="sons/son.mp3" autostart="false" ></audio>
<a onclick="PlaySound()"> Play </a>
<script>
function PlaySound() {
      var sound = document.getElementById("audio");
      sound.play()
  }
</script>

感谢您的回答。

1 个答案:

答案 0 :(得分:0)

你是在正确的方式来实现这一目标!

首先,您全局声明变量声音以从三个函数中获取它。要暂停声音,请使用.pause()方法并停止它,然后使用.currentTime属性返回0秒播放时间。

代码:

<audio id="audio" src="sound.mp3" autostart="false" ></audio>

<a href="#" onclick="PlaySound()">Play</a>
<a href="#" onclick="PauseSound()">Pause</a>
<a href="#" onclick="StopSound()">Stop</a>

<script>
    const sound = document.getElementById("audio")

    function PlaySound() {
        sound.play()
    }

    function PauseSound(){
        sound.pause()
    }

    function StopSound(){
        sound.pause()
        sound.currentTime = 0
    }
</script>

希望它可以帮到你!

相关问题