Syncing HTML5 <video> with <audio> playback

前端 未结 6 1470
你的背包
你的背包 2021-01-31 05:23

I am having track from one source, mute, and I\'d like to play background music over it using element. The tracks contain some time critical elements.

What would be th

6条回答
  •  死守一世寂寞
    2021-01-31 05:49

    Mikko Ohtamaa provided a solution in a comment, which I actually think is the best option here - it doesn't require a framework, and it doesn't require you to edit your video files.

    Essentially, just grab the current time from the video element when it is "unmuted", and apply that time to the audio element. Some code might look like this:

    function unmute() {
      var vid = document.getElementById("video");
      var aud = document.getElementById("audio");
    
      aud.currentTime = vid.currentTime;
      aud.play();
    
    }
    

提交回复
热议问题