user1475412 user1475412 - 1 year ago 90
Javascript Question

Synchronize video and audio (preferably without JavaScript)

If I have HTML5

elements, is there a clean way to keep them in sync? They should act like a video file that contains an audio track, so advancing one track manually should bring the other one along with it. Let's say the two tracks have the same duration.

I'd like a solution that works across browsers, but I don't particularly care if it works on older browsers. It would also be nice to avoid the use of JavaScript if possible. Otherwise, a dead-simple JavaScript library would be best -- something that only asks for which tracks to synchronize and takes care of the rest.

I've looked into mediagroup... but it looks like it only works in Safari. I've looked into audioTracks... but the user has to enable the feature in Firefox.

I've looked into Popcorn.js, which is a JavaScript framework that seems designed for this task... but it looks like there hasn't been any activity in over a year. Besides that, the only demonstrations I can find are of synchronizing things like text or slides to video, not audio to video.

Answer Source

You can use Promise.all(), fetch() to retrieve media resource as a Blob, URL.createObjectURL() to create a Blob URL of resource; canplaythrough event and Array.prototype.every() to check if each resource can play; call .play() on each resource when both resources can play

I didn't make it clear in the question. I meant that the two tracks should stay in sync as though playing a regular video file with audio.

One approach could be to create a timestamp variable, utilize seeked event to update .currentTime of elements when set variable is greater than a minimum delay to prevent .currentTime being called recursively.

<!DOCTYPE html>
  <button>load media</button>
  <div id="loading" style="display:none;">loading media...</div>
    var mediaBlobs = [];
    var mediaTypes = [];
    var mediaLoaded = [];
    var button = document.querySelector("button");
    var loading = document.getElementById("loading");
    var curr = void 0;
    var loadMedia = () => { = "block";
      button.setAttribute("disabled", "disabled");
      return Promise.all([
        // `RETURN` by smiling cynic are licensed under a Creative Commons Attribution 3.0 Unported License
      , fetch("")
        .then(responses => => ({
          [media.headers.get("Content-Type").split("/")[0]]: media.blob()
        .then(data => {
          for (var currentMedia = 0; currentMedia < data.length; currentMedia++) {
            (function(i) {
              var type = Object.keys(data[i])[0];
              var mediaElement = document.createElement(type);
     = type;
              var label = document.createElement("label");
              mediaElement.setAttribute("controls", "controls");
              mediaElement.oncanplaythrough = () => {
                if (mediaLoaded.length === data.length 
                   && mediaLoaded.every(Boolean)) {
           = "none";
                    for (var track = 0; track < mediaTypes.length; track++) {
              var seek = (e) => {
                if (!curr || new Date().getTime() - curr > 20) {
                    mediaTypes.filter(id => id !==[0]
                  ).currentTime =;
                  curr = new Date().getTime();
              mediaElement.onseeked = seek;
              mediaElement.onended = () => {
                for (var track = 0; track < mediaTypes.length; track++) {
              mediaElement.ontimeupdate = (e) => {
                .innerHTML = `${mediaTypes[i]} currentTime: ${Math.round(}<br>`;
              data[i][type].then(blob => {
                mediaElement.src = mediaBlobs[mediaBlobs.length - 1];
                document.body.insertBefore(label, mediaElement);
    button.addEventListener("click", loadMedia);


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