wired wired - 1 year ago 95
jQuery Question

Adding repeat option to audio player

I'm building audio player with jQuery. And I managed to do almost everything that I wanted, but I can not make song to repeat when I click on repeat button. It should repeat if I click on repeat button and if it is enabled, when I click again to disable it.
I init audio with

music = new Audio()

I tried to use some thing like:

$('.rep').on('click', function(){

but I can not get it work.

any advice, comment or tutorial would be helpful because I am looking to learn, not to copy/paste.


Answer Source

U can add eventListener like this.. :

    myAudio.addEventListener('ended', function() {
      this.currentTime = 0;
    }, false);

or just edit an attr loop like this CodePen :

myAudio = document.getElementById("myAudio"); 

    if(myAudio.loop != true){
      myAudio.loop = true;
    } else {
      myAudio.loop = false;
      myAudio.currentTime = 0;

Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download