YouTube Player API:如何在不播放的情况下获取加载/提示视频的持续时间? [英] YouTube Player API: How to get duration of a loaded/cued video without playing it?

查看:28
本文介绍了YouTube Player API:如何在不播放的情况下获取加载/提示视频的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过 YouTube 播放器 API 的 getDuration() 方法获取加载/提示视频的正确视频时长/长度(以秒为单位);但是,相同的方法会在视频开始播放后返回一个有效值!想知道 YouTube 如何能够显示加载/提示视频的有效时长.

当我用 15 秒的视频剪辑加载此 HTML 页面时,我得到以下调试输出:

状态 = 5 持续时间 = -0.000025

当我点击播放按钮时,我得到以下调试输出:

状态 = 3 持续时间 = 15,

非常感谢解决方案或变通方法.加载、立即播放和暂停播放器不是我最喜欢的方法.

<头><script type="text/javascript">var videoId;videoId = 'http://www.youtube.com/v/4TSJhIZmL0A';//英国广播公司//videoId = 'http://www.youtube.com/v/ezwyHNs_W_A';//物理函数 $(id) {返回 document.getElementById(id);}<script src="http://www.google.com/jsapi"></script><脚本>google.load("swfobject", "2.1");<身体><表格><tr><td><div id="玩家">您需要启用 Flash 播放器 8+ 和 JavaScript 才能观看此视频.

<脚本>var ytplayer;函数 myOnPlayerStateChange(state) {开关(状态){case 1://播放$("out").innerHTML += "正在播放";休息;case 2://暂停$("out").innerHTML += "暂停";休息;case 0://结束$("out").innerHTML += "已结束";休息;case -1://未启动case 3://缓冲case 5://提示$("out").innerHTML += " state = " + state;休息;默认值://未知$("out").innerHTML += " state = " + state;休息;}$("out").innerHTML += " duration = " + ytplayer.getDuration() + ",";}函数 myOnPlayerError(errorCode) {$("out").innerHTML += " 发生错误:" + errorCode;}功能 onYouTubePlayerReady(playerId) {ytplayer = ytplayer ||$(玩家ID);ytplayer.addEventListener("onStateChange", "myOnPlayerStateChange");ytplayer.addEventListener("onError", "myOnPlayerError");}var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };var atts = { };swfobject.embedSWF(videoId + "?border=0&amp;enablejsapi=1&amp;playerapiid=" + 'player', 'player', 425, 344, "8", null, null, params, atts);</td></tr><div id="out"></div><div id="错误"></div>

解决方案

解决方案 1

您可以使用 YouTube 数据 API 访问有关视频的大部分信息,包括时长:

<script type="text/javascript";src=http://gdata.youtube.com/feeds/api/videos/4TSJhIZmL0A?v=2&alt=jsonc&callback=youtubeFeedCallback&prettyprint=true"</script>

此处演示

使用 jQuery 时,您可以使用 $.getJSON() 使事情变得更容易.

解决方案 2

似乎 YouTube JavaScript API v3 允许您在 onYouTubePlayerReady() 事件中获得正确的持续时间.您需要做的就是在调用 swfobject.embedSWF() 方法时传递 &version=3.

此处演示

I'm not able to get the correct video duration/length (in seconds) of a loaded/cued video via the getDuration() method of the YouTube Player API; the same method, however, returns a valid value once the video starts playing! Wondering how YouTube is able to show the valid duration of a loaded/cued video.

When I load this HTML page with a 15 second video clip, I get the following debug output:

state = 5 duration = -0.000025

When I hit the Play button, I get the following debug output:

state = 3 duration = 15,

Would greatly appreciate a solution or a workaround. Loading, and immediately playing and pausing the player would be not my favorite method.

<html>
<head>
  <script type="text/javascript">
   var videoId;
   videoId = 'http://www.youtube.com/v/4TSJhIZmL0A';    // bbc
   // videoId = 'http://www.youtube.com/v/ezwyHNs_W_A'; // physics

    function $(id) {
      return document.getElementById(id);
    }
  </script>

  <script src="http://www.google.com/jsapi"></script>
  <script>
    google.load("swfobject", "2.1");
  </script>

</head>

<body>


  <table>
    <tr><td>
      <div id="player">
        You need Flash player 8+ and JavaScript enabled to view this video.
      </div>

    <script>
        var ytplayer;

        function myOnPlayerStateChange(state) {
          switch(state) {
            case 1:  // playing
              $("out").innerHTML += " playing";
              break;
            case 2:  // paused
              $("out").innerHTML += " paused";
              break;
            case 0:  // ended
              $("out").innerHTML += " ended";
              break;      

            case -1: // unstarted
            case 3:  // buffering
            case 5:  // cued
              $("out").innerHTML += " state = " + state;
              break;
            default: // unknown
              $("out").innerHTML += " state = " + state;
              break;
          }

          $("out").innerHTML += " duration = " + ytplayer.getDuration() + ",";
        }

        function myOnPlayerError(errorCode) {
          $("out").innerHTML += " Error occurred: " + errorCode;
        }

        function onYouTubePlayerReady(playerId) {
          ytplayer = ytplayer || $(playerId);
          ytplayer.addEventListener("onStateChange", "myOnPlayerStateChange");
          ytplayer.addEventListener("onError", "myOnPlayerError");
        }

        var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
        var atts = { };

        swfobject.embedSWF(videoId + "?border=0&amp;enablejsapi=1&amp;playerapiid=" + 'player', 'player', 425, 344, "8", null, null, params, atts);
    </script>
    </td></tr>
  </table>
  <div id="out"></div>
  <div id="err"></div>
</body>
</html>

解决方案

Solution 1

You can use YouTube Data API to access most of the information about the video, including duration:

<script type="text/javascript">
    function youtubeFeedCallback(json){
        document.write(json["data"]["duration"] + " second(s)");
    }
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/4TSJhIZmL0A?v=2&alt=jsonc&callback=youtubeFeedCallback&prettyprint=true"></script>

Demo here

When using jQuery you can use $.getJSON() to make things easier.

Solution 2

Seems like YouTube JavaScript API v3 allows you to get the correct duration inside the onYouTubePlayerReady() event. All you need to do is pass &version=3 when calling swfobject.embedSWF() method.

Demo here

这篇关于YouTube Player API:如何在不播放的情况下获取加载/提示视频的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆