How to make an audio stop when you start another one in html5?

0

Good on a page with

asked by Criss 28.11.2017 в 14:51
source

2 answers

0

You just have to pass the id of audio that you want to pause and ready friend

var audio = document.getElementById("myAudio"); 

function playAudio() { 
audio.play(); 
} 

function pauseAudio() { 
audio.pause(); 
}
    
answered by 28.11.2017 в 14:57
0

There is an event that allows you to detect when a video / audio is playing. For simplicity I give you an example with jQuery how to stop the rest of the videos:

$('audio').on('play', function () { //cuando un audio empieza a reproducirse
  var current=this;
  $('audio').each(function() { 
    if (this!== current) { //todos los demás
      this.pause();  //los pausamos
    }
   });
});

Making the audio do not stop when loading another page is impossible, since it is part of the page you are leaving. The most you can do is detect when the user leaves the page, save the playback time (attribute currentTime ) in the localStorage and try to play from that moment on the new page, which must load the same resource. But anyway there will be a pause between both events.

    
answered by 28.11.2017 в 15:16