YouTube 播放器 API:如何在不播放的情况下获取已加载/提示视频的持续时间?
我无法通过 YouTube Player API 的 getDuration() 方法获取已加载/提示视频的正确视频持续时间/长度(以秒为单位);然而,一旦视频开始播放,相同的方法就会返回一个有效值!想知道 YouTube 如何显示已加载/提示视频的有效持续时间。
当我加载包含 15 秒视频剪辑的 HTML 页面时,我得到以下调试输出:
state = 5uration = -0.000025
当我点击“播放”按钮时,我得到以下调试输出:
>状态= 3持续时间= 15,
非常感谢解决方案或解决方法。加载、立即播放和暂停播放器不是我最喜欢的方法。
<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&enablejsapi=1&playerapiid=" + 'player', 'player', 425, 344, "8", null, null, params, atts);
</script>
</td></tr>
</table>
<div id="out"></div>
<div id="err"></div>
</body>
</html>
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&enablejsapi=1&playerapiid=" + 'player', 'player', 425, 344, "8", null, null, params, atts);
</script>
</td></tr>
</table>
<div id="out"></div>
<div id="err"></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方案 1
您可以使用 YouTube 数据 API 访问有关视频的大部分信息,包括时长:
此处演示
使用 jQuery 时,您可以使用
$.getJSON()
让事情变得更容易。解决方案 2
YouTube JavaScript API v3 似乎允许您在
onYouTubePlayerReady()
事件中获取正确的持续时间。您所需要做的就是在调用swfobject.embedSWF()
方法时传递&version=3
。此处演示
Solution 1
You can use YouTube Data API to access most of the information about the video, including duration:
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 callingswfobject.embedSWF()
method.Demo here
按照以下步骤操作:
使用 Jquery
Following these steps:
Using Jquery:
“加载、立即播放和暂停播放器不是我最喜欢的方法。”
我认为没有其他方法。
就我而言,它效果很好,即使对于卫星互联网用户也是如此。
这是我的代码:
"Loading, and immediately playing and pausing the player would be not my favorite method."
I don't think there is any other method.
In my case it works well, even for a satellite internet user.
Here is my code :
这里有一个可以为您转换时间的工具。
Here one that converts the time over for you.
使用 jQuery
Using jQuery