播放所选视频

时间:2012-07-17 13:06:10

标签: javascript html5-video playlist

    <script type="text/javascript">
function playVideo(sourceId, targetId) {
   if (typeof(sourceId)=='string') {sourceId=document.getElementById(sourceId);}
   if (typeof(targetId)=='string') {targetId=document.getElementById(targetId);}
   targetId.innerHTML=sourceId.innerHTML;
   return false;

   }
    </script>
<video id="6" width="320" height="240" controls="controls"></video>

<video id="1" style="display: none;"width="320" height="240" controls="controls">
  <source src="movie1.mp4" type="video/mp4" />
  <source src="movie1.ogg" type="video/ogg" /> 
  <source src="movie1.webm" type="video/webm" />
Your browser does not support the video tag.
</video>
<video id="2" style="display: none;" width="320" height="240" controls="controls">
  <source src="movie2.mp4" type="video/mp4" />
  <source src="movie2.ogg" type="video/ogg" /> 
  <source src="movie2.webm" type="video/webm" />
Your browser does not support the video tag.
</video>


<a href="#" onclick='return playVideo("1","6")'>Play Video 1</a>
<a href="#" onclick='return playVideo("2","6")'>Play Video 2</a>

当视频1正在播放视频2时选择“播放视频2”时无效,我该如何解决?我是否需要添加其他脚本,以便第二个视频覆盖第一个?

2 个答案:

答案 0 :(得分:0)

playVideo功能的工作方式是......不直观。

使用videoElement.play();播放视频。例如,document.getElementById('1').play();。要停止播放视频,请使用stop();

Tutorial / docs

答案 1 :(得分:0)

<script type="text/javascript">
    function playVideo(sourceId, targetId) {
        if (typeof(sourceId)=='string') {sourceId=document.getElementById(sourceId);}
        if (typeof(targetId)=='string') {targetId=document.getElementById(targetId);}
        targetId.innerHTML=sourceId.innerHTML;
        return false;
    }
</script>

<div id="6"></div>

<div id="1" style="display: none">
    <video width="320" height="240" controls="controls">
        <source src="movie1.mp4" type="video/mp4" />
        Your browser does not support the video tag.
    </video>
</div>

<div id="2" style="display: none">
    <video width="320" height="240" controls="controls">
        <source src="movie2.avi" type="video/avi" />
        Your browser does not support the video tag.
    </video>
</div>

<a href="#" onclick='return playVideo("1","6")'>Play Video 1</a>
<a href="#" onclick='return playVideo("2","6")'>Play Video 2</a>

我做了什么:我将两个视频元素放在隐形divs中。那些divs有ID(1&amp; 2),divs(您的视频元素)的内容被加载到ID为6的第三个(可见)div