如何将音频源与外部视频同步并在两者之间交叉淡入淡出?

时间:2013-09-03 22:40:55

标签: javascript html5 html5-video html5-audio popcornjs

我需要一个适用于Chrome的解决方案,可以接受任何YouTube,Vimeo,SoundCloud或自定义网址。

我有一种感觉Popcorn.js可以帮助我完成这项任务。

1 个答案:

答案 0 :(得分:1)

这是使用Popcorn.js和jQuery的a solution

的index.html

<div id="choose">
  <p>Audio URL:
    <input type="text" id="audio-url" />
    <br/>(Example: http://thelab.thingsinjars.com/web-audio-tutorial/hello.mp3)</p>
  <br/>
  <br/>
  <p>Video URL:
    <input type="text" id="video-url" />
    <br/>( Example: http://vimeo.com/73605534)</p>
  <br/>
  <br/>
  <button id="doit">Do It!</button>
</div>
<div id="dopop">
  <button id="toggle">Play/Pause</button>
  <br/>
  <br/>
  <input type="range" min="0" max="100" name="range" id="range" />
  <br/>
  <div id="source1">
    <p> <span class="gain">Source 1 Gain: 1.0</span>

        <br/>
        <div id="audio"></div>
    </p>
  </div>
  <hr/>
  <div id="source2">
    <p><span class="gain">Source 2 Gain: 1.0</span>

        <br/>
        <div id="video"></div>
    </p>
  </div>
</div>

内联脚本:

function doPop() {
  $("#choose").hide();
  $("#dopop").show();
  // create an instance of popcorn
  var popcorn = Popcorn.smart("#audio", $("#audio-url").val());
  var popcorn2 = Popcorn.smart("#video", $("#video-url").val());

  popcorn.on('play', function () {
    popcorn2.play();
  });

  popcorn.on('pause', function () {
    popcorn2.pause();
  });

  popcorn2.on('play', function () {
    popcorn.play();
  });

  popcorn2.on('pause', function () {
    popcorn.pause();
  });

  var clicked = false;

  $("#toggle").click(function () {
    if (clicked) {
        popcorn.pause();
        clicked = false;
    } else {
        popcorn.play();
        clicked = true;
    }
  });

  $("#range").change(function () {
    var x = parseInt($(this).val()) / 100;
    // Use an equal-power crossfading curve:
    var gain1 = Math.cos(x * 0.5 * Math.PI);
    var gain2 = Math.cos((1.0 - x) * 0.5 * Math.PI);
    popcorn.volume(gain1);
    popcorn2.volume(gain2);
    $("#source1 .gain").text("Source 1 Gain: " + gain1);
    $("#source2 .gain").text("Source 2 Gain: " + gain2);
  });

}

$("#doit").click(function () {
  doPop();
});
相关问题