Youtube API seekTo()in hh:mm:ss格式

时间:2015-09-26 17:28:27

标签: javascript youtube-api

我使用基于youtube api的seekto来创建一个当前视频的链接,但使用秒,我想使用小时分秒(HHMMSS)。 使用一点点javascript肯定可以吗?

我的代码:https://jsfiddle.net/94150148/1pfat4wu/

<iframe id="player" type="text/html" width="500" height="400"
 src="https://www.youtube.com/embed/Zou31ZBBhTY?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<br>
<p><a href="javascript:void(0);" onClick="player.seekTo(60)">Click here</a></p>
<p><a href="javascript:void(0);" onClick="player.seekTo(120)">Click here</a></p>

<script>
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');
    }
</script>

由于

2 个答案:

答案 0 :(得分:1)

我不确定Youtupe API是否支持HH:MM:SS寻求支持,但您当然可以将HH:MM:SS转换为Seconds,即:

<强> HTML:

<iframe id="player" type="text/html" width="500" height="400" src="https://www.youtube.com/embed/Zou31ZBBhTY?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<br>
<p><a href="javascript:void(0);" onClick="player.seekTo(convertMToS('01:12:26'))">01:12:26</a></p>
<p><a href="javascript:void(0);" onClick="player.seekTo(convertMToS('00:55:55'))">00:55:55</a></p>

<强> JS:

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');
}

function convertMToS(hms){
// your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 
console.log(seconds);
return seconds;
}

<强>样本: https://jsfiddle.net/1pfat4wu/9/

答案 1 :(得分:0)

如果时间码格式可以是DD:HH:MM:SSHH:MM:SSMM:SSSS中的任何一种,请使用此选项。

var duration = '3:23:59:59'; // 4 days minus 1 second

var a = duration.split(':');
var mul = new Array(1, 60, 3600, 86400);

var seconds = 0;

for (var x = 0; x < a.length; x++)
{
    seconds += (a[a.length - 1 - x] * mul[x]);
}