12

iframe video在我的网页中使用了一个。这是我的html代码

<iframe id="video1" width="520" height="360" src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi" frameborder="0" allowtransparency="true" allowfullscreen></iframe>
<a href="#" id="playvideo">Play video</a>

我需要play视频onclick播放视频链接。我怎样才能做到这一点?

4

6 回答 6

29

这有效,它附加autoplay=1到导致视频开始播放的 url。

附录:如果您的视频源还没有查询字符串,那么添加 a?而不是 a是谨慎的&做法,有时会出现这种情况。这可以通过寻找它的存在来完成。

<iframe id="video1" width="520" height="360" src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi" frameborder="0" allowtransparency="true" allowfullscreen></iframe>
<a href="#" id="playvideo">Play video</a>
<script>
 //use .one to ensure this only happens once
 $("#playvideo").one(function(){
  //as noted in addendum, check for querystring exitence
  var symbol = $("#video1")[0].src.indexOf("?") > -1 ? "&" : "?";
  //modify source to autoplay and start video
  $("#video1")[0].src += symbol + "autoplay=1";
 });
</script>

但是,大多数人天生就明白,如果他们想要播放视频,他们只需点击它,我建议将其留给他们或通过自动播放开始视频。

还需要提到自动播放在移动设备上不起作用(由 Android 或 iOS 提供支持)

于 2012-11-29T05:08:09.660 回答
5

我正确设置了 end src - ?autoplay=1

   <iframe id="video1" width="450" height="280" src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi" frameborder="0" allowtransparency="true" allowfullscreen></iframe>
    <a href="#" id="playvideo">Play button</a>
    <script>
     $("#playvideo").click(function(){
      $("#video1")[0].src += "?autoplay=1";
     });
    </script>
于 2017-05-19T11:00:32.787 回答
3

这是另一个例子。在这里检查:https ://codepen.io/rearmustak/pen/VXXOBr

const Player = document.getElementById('player2');
const PlayBtn = document.getElementById('play');
const stopBtn = document.getElementById('stop');
let times = 0, playY;
const playVideo = PlayBtn.addEventListener( 'click' , () => {
    if(times == 0){
      playY = Player.src += '?autoplay=1';
      times = 1;
    }
});

stopBtn.addEventListener( 'click' , () => {
    playY = playY.slice(0, -11);
    Player.src = playY
    times = 0;
});
.video-frame{
  overflow: hidden;
  margin-bottom: 10px;
}
button{
  border: none;
  background-color: #e75252;
  color: #ffffff;
  padding: 10px 15px;
  border-radius: 3px;
  cursor: pointer;
}
button:focus{
  outline: 0;
}
#stop{
  background-color: #ff0002;
}
<h1>Youtube video Play/Stop</h1>

<div class="video-frame">
  <iframe id="player2" width="560" height="315" src="https://www.youtube.com/embed/cs1e0fRyI18" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>

<button id="play">Play</button>
<button id="stop">Stop</button>

于 2018-03-31T05:26:30.253 回答
2

由于第一个答案已经有 3 年的历史了,让我指出 Youtube Player API。有了它,您可以远程控制您的嵌入式播放器。请参阅https://developers.google.com/youtube/iframe_api_reference?hl=en

稍作调整,您就可以通过链接启动视频,而无需重新加载整个 iframe:

<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <!-- The Play-Link will appear in that div after the video was loaded -->
    <div id="play"></div>

    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        //event.target.playVideo();
        document.getElementById('play').innerHTML = '<a href="#" onclick="play();">Play Video</a>';
      }

      function play(){
        player.playVideo();
      }

      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
  </body>
</html>
于 2015-11-25T08:42:52.333 回答
0

这是指向示例http://jsfiddle.net/WYwv2/5/的链接

看看这个

<!DOCTYPE html> 
<html> 
<body> 
<a href="#" id="playvideo" onclick="playme()">Play video</a>
    <iframe id="video1" width="520" height="360"  frameborder="0" ></iframe>
<script>
function playme() {
document.getElementById("video1").src = 'http://www.w3schools.com/tags/mov_bbb.mp4';
}
</script>
</body> 
</html>
​

我们动态设置视频来源。

于 2012-11-29T05:21:14.250 回答
0

这对我有用。请看看这个。

$("#player").on("pause", function(e) {
          clearTimeout(playTimeout);
      });
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
      var player;
        function onYouTubeIframeAPIReady() {
          player = new YT.Player('player', {
            height: '390',
            width: '640',
            videoId: 'M7lc1UVf-VE',
            events: {
              'onReady': onPlayerReady,
              'onStateChange': onPlayerStateChange
            }
          });
        }
      function onPlayerReady(event) {
        event.target.playVideo();
          }
       var done = false;
        function onPlayerStateChange(event) {
          if (event.data == YT.PlayerState.PLAYING && !done) {
            setTimeout(stopVideo, 2000);//replace time here 
            $("#player").get(0).load();
            $("#player").get(0).pause();
            done = true;
          }
        }
        function stopVideo() {
          player.stopVideo();
        }
于 2020-12-17T06:12:08.540 回答