wired wired - 2 months ago 11
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(){
music.prop('loop');
})


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.

Thanks

Answer

U can add eventListener like this.. :

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

or just edit an attr loop like this CodePen :

myAudio = document.getElementById("myAudio"); 

$('.rep').click(function(){
    if(myAudio.loop != true){
      myAudio.loop = true;
      myAudio.play();
    } else {
      myAudio.loop = false;
      myAudio.currentTime = 0;
      myAudio.pause();
    }
});

Hope it helps.

Comments