我使用基于youtube api的seek来创建一个链接到视频的时刻,但使用秒,我想使用而不是小时分钟秒(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>
谢谢
我不确定Youtupe API
是否支持HH:MM:SS
寻求,但您肯定可以将HH:MM:SS
转换为Seconds
,即:
<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/
如果时间码格式可以是DD:HH:MM:SS
、HH:MM:SS
、MM:SS
或SS
中的任何一种,则使用此方法。
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]);
}