If you want to synchronize the audios and sound at the same time, the idea would be for your program to use the event canplaythrough
and do the following:
- Defines a variable that will serve as a counter for the audios loaded.
- Create the two audios on the without autoplay page.
- Put an event handler
canplaythrough
in the audios.
- When the audio is finished loading, the controller will trigger, which will:
- Add +1 to the audio counter loaded.
- Have all audios been loaded?
- If yes, play the audios at the same time.
Why use the event canplaythrough
instead of loadeddata
or canplay
? The canplaythrough
event is launched when enough data has been loaded to know that the entire melody can be played without buffering. Something that could cause the audios to desynchronize.
Here I leave a commented example of two different audios (although quite dissonant, sorry) that sound simultaneously synchronized:
// contador de audios cargados
var cargados = 0;
// controlador del evento canplaythrough
function sincronizarAudio() {
// aumentamos el número de audios que se han cargado
cargados++;
// si se han cargado los dos audios que queremos sincronizar
if (cargados == 2) {
// le damos play a los dos
audio1.play();
audio2.play();
}
}
// crea un elemento audio
var audio1 = document.createElement("audio");
// asigna la fuente del sonido
audio1.setAttribute("src", "http://www.w3schools.com/tags/horse.ogg");
// opcional: especifica que se vean los controles
audio1.setAttribute("controls", "controls");
// añade el controlador del evento canplaythrough
audio1.oncanplaythrough = sincronizarAudio;
// añade el elemento al cuerpo de la página
document.querySelector("body").appendChild(audio1);
// lo mismo de arriba pero con otro audio/video
var audio2 = document.createElement("audio");
audio2.setAttribute("src", "http://www.w3schools.com/tags/movie.ogg");
audio2.setAttribute("controls", "controls");
audio2.oncanplaythrough = sincronizarAudio;
document.querySelector("body").appendChild(audio2);
If you also want to synchronize when pause / play, you can add event handlers for pause
and play
that would apply the method pause()
and play()
respectively to all synchronized audios .
In the following example I used two slightly longer audios (although of different duration, which can cause problems) so you can see how when pausing / playing in one of them the other also pauses / plays.
var cargados = 0;
function sincronizarAudio() {
cargados++;
if (cargados == 2) {
audio1.play();
audio2.play();
}
}
function sincronizarPlay() {
audio1.play();
audio2.play();
}
function sincronizarPause() {
audio1.pause();
audio2.pause();
}
var audio1 = document.createElement("audio");
audio1.setAttribute("src", "http://www.w3schools.com/tags/mov_bbb.ogg");
audio1.setAttribute("controls", "controls");
audio1.oncanplaythrough = sincronizarAudio;
audio1.onplay = sincronizarPlay;
audio1.onpause = sincronizarPause;
document.querySelector("body").appendChild(audio1);
var audio2 = document.createElement("audio");
audio2.setAttribute("src", "http://www.w3schools.com/tags/movie.ogg");
audio2.setAttribute("controls", "controls");
audio2.oncanplaythrough = sincronizarAudio;
audio2.onplay = sincronizarPlay;
audio2.onpause = sincronizarPause;
document.querySelector("body").appendChild(audio2);