"Play / Pause" button - How to restart playback?

1

I made a simple "play / pause" button for audio, with the following script:

<audio id="myAudio"         
src="http://www.sousound.com/music/healing/healing_01.mp3"preload="auto">
</audio>

<script type="text/javascript" charset="utf-8" async defer>
var myAudio = document.getElementById("myAudio");
var isPlaying = false;

function togglePlay() {
if (isPlaying) {
myAudio.pause()
} else {
myAudio.play();
}
};
myAudio.onplaying = function() {
isPlaying = true;
};
myAudio.onpause = function() {
isPlaying = false;
};
</script>    

However, I would actually like that by clicking the " play / pause " button instead of pausing, I would restart playback. Any light?

    
asked by anonymous 29.11.2017 / 13:29

1 answer

1

Add myAudio.currentTime = 0; to function myAudio.onpause :

var myAudio = document.getElementById("myAudio");
var isPlaying = false;

function togglePlay() {
   if (isPlaying) {
      myAudio.pause()
   } else {
     myAudio.play();
   }
};

myAudio.onplaying = function() {
   isPlaying = true;
};
myAudio.onpause = function() {
   myAudio.currentTime = 0;
   isPlaying = false;
};
<audio id="myAudio" preload="auto" controls>
   <source src="http://www.sousound.com/music/healing/healing_01.mp3"type="audio/mpeg">
</audio>
    
29.11.2017 / 13:46