Shane Shane -5 years ago 212
HTML Question

How to toggle audio play() pause() with one button or link?

I have an audio file that plays when an anchor tag is clicked. If the anchor tag is clicked again, I want the audio to pause, I just don't know enough about javascript to pull this second half off. I don't want to change the content of the anchor tag they click, I just want the audio file to start and pause whenever they click the tag.

This is what I have so far, which at least makes the sound file playable:

<audio id="audio" src="/Demo.mp3"></audio>
<a onClick="document.getElementById('audio').play()">Click here to hear.</a>

Answer Source

You could use jQuery to make a toggle for this.

<a id="music-button" style="cursor:pointer;">
<img src=""></a>
<audio id="playMusic" autoplay>
<source src="sound.mp3">

<script type="text/javascript">
function () {
function () {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download