问题描述
如何获取YouTube视频的当前时间?
How can I get the currentTime of a YouTube video?
我一直在YouTube api中搜索,我只是不明白:
I have been searching in the YouTube api and I just do not understand:https://developers.google.com/youtube/js_api_reference?csw=1
我一直在Stack中搜索并尝试应用他们所说的但不起作用。可能我错过了一些我不理解的东西:
I have been searching in Stack and try to apply what they say but it does not work. Problably I am missing something that I do not understand:
Youtube API returning current time
Getting Current YouTube Video Time
我很感激任何指导,知道从哪里开始
I would appreciate any guidence to know where to begin
你可以检查自己:
$("#bookmarkBoto").click(function() {
//var time = $("#yt").currentTime();//no
//var time = $("#yt").getCurrentTime();//no
$('#check').text(time);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="yt" name="yt" width="720" height="405" src="https://www.youtube.com/embed/M7lc1UVf-VE" frameborder="0" allowfullscreen></iframe>
<br><br>
<div id="bookmarkBoto">bookmarkBoto</div>
<div id="check">check</div>
推荐答案
没有样本方式,您需要使用。这是javascript代码。我最后做了一个实例。
No sample way, you need to use the YouTube API. This is the javascript code. I made a live example at the end.
JAVASCRIPT
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
$('button').click(function() {
console.log(player.getCurrentTime())
})
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<iframe id="player" width="640" height="360" src="https://www.youtube.com/embed/zua0_IXcPFY?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<button>click time</button>
</body>
</html>
这篇关于使用jQuery的YouTube currentTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!