Loop audio with JavaScript

前端 未结 3 654
眼角桃花
眼角桃花 2021-01-21 16:35

I have the next code

var audioElement0 = document.createElement(\'audio\');
audioElement0.setAttribute(\'src\', \'notify.wav\');
audioElement0.setAttribute(\'aut         


        
相关标签:
3条回答
  • 2021-01-21 16:59

    The audio element has a loop boolean property that you can set to auto-loop its playback. Another option would be to add and event listener which responds to the "ended" event of the audio element. In your handler, set the position of the audio back to 0, and play anew.

    0 讨论(0)
  • 2021-01-21 17:06

    Set the loop="loop" attribute in the audio tag.

    0 讨论(0)
  • 2021-01-21 17:23

    You have the loop property:

    audioElement.loop=true;

    But some browsers do not support well the loop property, you can add an event listener like this:

    audioElement.addEventListener('ended', function() {
        this.currentTime = 0;
        this.play();
    }, false);
    
    0 讨论(0)
提交回复
热议问题