如何在iframe嵌入视频播放,暂停,缓冲时显示提醒

时间:2013-12-24 11:53:34

标签: php jquery youtube youtube-api

当我们使用jquery点击它时,我用相应的iframe替换特定的div内容,如何在动态加载iframe播放,暂停,缓冲时显示警报。

任何人都可以告诉我如何解决这个问题..

2 个答案:

答案 0 :(得分:1)

Try This Example : 
  <html>
        <head>
            <script src = "http://www.youtube.com/player_api"></script>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link rel="stylesheet" href="style.css" type="text/css"     media="screen" charset="utf-8">
        </head>
        <body dir = "rtl">
            <iframe id="player" width="640" height="360"   src="http://www.youtube.com/embed/-cSFPIwMEq4" onload="floaded()" frameborder="0" allowfullscreen="">
            </iframe>
    <script>
    //onYouTubePlayerAPIReady
    var player;
      function floaded(){
        player = new YT.Player('player', {
          videoId: '-cSFPIwMEq4',
          events:
           {      
            'onStateChange': function (event)
                {
                if (event.data == YT.PlayerState.PLAYING)                                    {  alert("Playing.."); }
                else if (event.data == YT.PlayerState.PAUSED)
                   { alert ("Paused.."); }
                else
                alert ("Buffering/Video Ended");                

                 }
           }

        });
      }

    </script>

    </body>
    </html>

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
   <!-- <div id="player"></div>-->
<iframe id="player" type="text/html" width="640" height="390"
  src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1"
  frameborder="0"></iframe>
    <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();
      }

      // 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) {
          alert('Video Playing');
   
        }
		else if (event.data == YT.PlayerState.PAUSED) {
          alert('Video Paused');
   
        }}
    </script>
  </body>
</html>