将HTML5 <video>与<audio>回放</audio> </video>同步

时间:2011-06-22 01:26:31

标签: javascript html5 video audio

我正在从一个来源跟踪,静音,我想使用元素播放背景音乐。这些曲目包含一些时间关键元素。

在HTML5 / Javascript中同步这两个不同媒体播放器的选项是什么?会给出主时钟,因为音频播放对时间非常敏感 - 偶尔丢失视频帧并不重要。

6 个答案:

答案 0 :(得分:12)

Mikko Ohtamaa在评论中提供了一个解决方案,我认为这是最好的选择 - 它不需要框架,也不需要您编辑视频文件。

基本上,只需在“取消静音”时从视频元素中获取当前时间,然后将该时间应用于音频元素。有些代码可能如下所示:

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

  aud.currentTime = vid.currentTime;
  aud.play();

}

答案 1 :(得分:2)

Here是使用受this文章启发的Popcorn.js的简单解决方案。

$(function(){
var medias = {
    audio: Popcorn("#narration"),
    video: Popcorn("#video")
  },
  loadCount = 0,
  events = "play pause timeupdate seeking volumechange".split(/\s+/g); 


// iterate both media sources
Popcorn.forEach(medias, function(media, type) {

  // when each is ready... 
  media.on("canplayall", function() {

    // trigger a custom "sync" event
    this.emit("sync");

    // Listen for the custom sync event...    
  }).on("sync", function() {

    // Once both items are loaded, sync events
    if (++loadCount == 2) {
      // Uncomment this line to silence the video
      //medias.video.mute();

      // Iterate all events and trigger them on the video 
      // whenever they occur on the audio
      events.forEach(function(event) {

        medias.video.on(event, function() {

          // Avoid overkill events, trigger timeupdate manually
          if (event === "timeupdate") {

            if (!this.media.paused) {
              return;
            }
            medias.audio.emit("timeupdate");

            return;
          }

          if (event === "seeking") {
              medias.audio.currentTime(this.currentTime());
          }
          
          if (event === "volumechange") {  
        	  if(this.muted()) { 
        		  medias.audio.mute(); 
        	  } else {
        		  medias.audio.unmute(); 
        	  } 
        	  
        	  medias.audio.volume(this.volume());
          }

          if (event === "play" || event === "pause") {
            medias.audio[event]();
          }
        });
      });
    }
  });
});
});
  	<script type="text/javascript" src="http://code.jquery.com/jquery-git.js"></script>
  	<script type="text/javascript" src="https://static.bocoup.com/js/popcorn.min.js"></script>
     
<audio id="narration">
	<source src="https://videos.cdn.mozilla.net/uploads/webmademovies/popcorn101/popcorn101.ogg">
</audio>
<video id="video" controls="controls">  
	<source src="https://videos.cdn.mozilla.net/uploads/webmademovies/popcorntest.mp4">
</video>

答案 2 :(得分:1)

通过一个非常简单的技巧轻松;)

<video id="myvideo" controls muted loop>
    <source src="sample_video_no_sound.mp4" type="video/mp4"/>
    <audio id="myaudio">
        <source src="sound.mp3" type="audio/mpeg"/>
    </audio>
</video>

<script>
    var myvideo = document.getElementById("myvideo");
    var myaudio = document.getElementById("myaudio");

    var change_time_state = true;

    myvideo.onplay = function(){
        myaudio.play();
        if(change_time_state){
            myaudio.currentTime = myvideo.currentTime;
            change_time_state = false;
        }
    }

    myvideo.onpause = function(){
        myaudio.pause();
        change_time_state = true;
    }
</script>

答案 3 :(得分:0)

将音乐包含在视频文件中也可以达到同样的效果。 html5视频播放器控件可以使声音静音。

答案 4 :(得分:0)

试试VideoJS。当页面加载并准备就绪时视频播放开始时,您可以触发一个功能,该功能将开始单独播放页面中包含的音乐。

http://videojs.com/

答案 5 :(得分:0)

没有可靠的解决方案。支持多个流同步的唯一html5功能是mediaGroup。它没有被Chrome部署,并被w3c弃用。

相关问题