【问题标题】:YouTube JavaScript API - Play videos without having to create Iframes on mobile phones?YouTube JavaScript API - 无需在手机上创建 iframe 即可播放视频?
【发布时间】:2013-06-06 08:09:43
【问题描述】:

youtube javascript api 中是否没有选项可以在手机上启动视频(通过 id/url)?现在我有一个视频列表(通过 php 从 youtube api 下载的数据),我正在显示每个视频的缩略图,旁边有标题/日期/等。

理想情况下,我希望在用户点击缩略图时播放视频,但如果不让 JS api 创建视频的 iframe(在叠加层中),我无法弄清楚如何做到这一点,但是用户有再次点击该叠加层以播放视频,因为 api 不允许在手机上自动播放。

有人知道解决办法吗?

【问题讨论】:

    标签: javascript youtube youtube-api


    【解决方案1】:

    如果您使用的是 Android,则应使用 Android Player。 对于其他任何事情,您应该使用IFrame Player。 由于Apple's own terms,自动播放在 iOS 中无法运行。

    即使您找到了解决方法,也违反了这些条款,我不建议这样做。

    【讨论】:

      【解决方案2】:

      fiddle。代码如下:

      <div id="timerText"></div>
      <iframe id="player" width="420" height="315" src="http://www.youtube.com/embed/TP2iRzzamCk" frameborder="0" allowfullscreen></iframe>
      
      <body >
      
      <script>
      /**
       * @author       Rob W <gwnRob@gmail.com>
       * @website      http://stackoverflow.com/a/7513356/938089
       * @version      20120724
       * @description  Executes function on a framed YouTube video (see website link)
       *               For a full list of possible functions, see:
       *               https://developers.google.com/youtube/js_api_reference
       * @param String frame_id The id of (the div containing) the frame
       * @param String func     Desired function to call, eg. "playVideo"
       *        (Function)      Function to call when the player is ready.
       * @param Array  args     (optional) List of arguments to pass to function func*/
      function callPlayer(frame_id, func, args) {
          if (window.jQuery && frame_id instanceof jQuery) frame_id = frame_id.get(0).id;
          var iframe = document.getElementById(frame_id);
          if (iframe && iframe.tagName.toUpperCase() != 'IFRAME') {
              iframe = iframe.getElementsByTagName('iframe')[0];
          }
      
          // When the player is not ready yet, add the event to a queue
          // Each frame_id is associated with an own queue.
          // Each queue has three possible states:
          //  undefined = uninitialised / array = queue / 0 = ready
          if (!callPlayer.queue) callPlayer.queue = {};
          var queue = callPlayer.queue[frame_id],
              domReady = document.readyState == 'complete';
      
          if (domReady && !iframe) {
              // DOM is ready and iframe does not exist. Log a message
              window.console && console.log('callPlayer: Frame not found; id=' + frame_id);
              if (queue) clearInterval(queue.poller);
          } else if (func === 'listening') {
              // Sending the "listener" message to the frame, to request status updates
              if (iframe && iframe.contentWindow) {
                  func = '{"event":"listening","id":' + JSON.stringify(''+frame_id) + '}';
                  iframe.contentWindow.postMessage(func, '*');
              }
          } else if (!domReady || iframe && (!iframe.contentWindow || queue && !queue.ready)) {
              if (!queue) queue = callPlayer.queue[frame_id] = [];
              queue.push([func, args]);
              if (!('poller' in queue)) {
                  // keep polling until the document and frame is ready
                  queue.poller = setInterval(function() {
                      callPlayer(frame_id, 'listening');
                  }, 250);
                  // Add a global "message" event listener, to catch status updates:
                  messageEvent(1, function runOnceReady(e) {
                      var tmp = JSON.parse(e.data);
                      if (tmp && tmp.id == frame_id && tmp.event == 'onReady') {
                          // YT Player says that they're ready, so mark the player as ready
                          clearInterval(queue.poller);
                          queue.ready = true;
                          messageEvent(0, runOnceReady);
                          // .. and release the queue:
                          while (tmp = queue.shift()) {
                              callPlayer(frame_id, tmp[0], tmp[1]);
                          }
                      }
                  }, false);
              }
          } else if (iframe && iframe.contentWindow) {
              // When a function is supplied, just call it (like "onYouTubePlayerReady")
              if (func.call) return func();
              // Frame exists, send message
              iframe.contentWindow.postMessage(JSON.stringify({
                  "event": "command",
                  "func": func,
                  "args": args || [],
                  "id": frame_id
              }), "*");
          }
          /* IE8 does not support addEventListener... */
          function messageEvent(add, listener) {
              var w3 = add ? window.addEventListener : window.removeEventListener;
              w3 ?
                  w3('message', listener, !1)
              :
                  (add ? window.attachEvent : window.detachEvent)('onmessage', listener);
          }
      }
          var secs = 10;
          var currentSeconds = 0;
          var currentMinutes = 0;
          setTimeout('Decrement()',1000);
      
          function Decrement() {
              currentMinutes = Math.floor(secs / 60);
              currentSeconds = secs % 60;
              if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
              secs--;
              document.getElementById("timerText").innerHTML = currentMinutes + ":" + currentSeconds; //Set the element id you need the time put into.
              if(secs !== -1) {
                  setTimeout('Decrement()',1000);
              }else{
                  callPlayer('player', 'playVideo');
              }
          }
      </script>
      

      【讨论】:

      • 适用于平板电脑/台式机,不适用于手机(例如 iPhone)。不过没有检查 Android/Windows Phone。
      • @Joe 好吧,该 API 适用于安装了 Flash 播放器的平台(因此并非适用于所有智能手机/平板电脑),无论如何都有可能将该 API 与 iOS 一起使用,但我想在这种情况下不会.只为举报groups.google.com/forum/?fromgroups#!topic/youtube-api-gdata/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 2012-03-19
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 2018-10-12
      相关资源
      最近更新 更多