YouTube iFrame API .seekTo()不是一种方法? [英] YouTube iFrame API .seekTo() not a method?
问题描述
我知道.seekTo()适用于Javascript API,但我无法使用 iFrame API。这种方法是否受支持?下面的代码成功嵌入了视频,并成功地将console.log -s作为播放器对象。我在控制台中收到错误player.seekTo不是函数。
I know that .seekTo() works in the Javascript API, but I'm not able to get it working with the iFrame API. Is this method supported? The code below successfully embeds the video and successfully console.log-s the player object. I get the error "player.seekTo is not a function" in the console.
<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'u1zgFlCw8Aw'
});
}
$(window).load(function(){
jQuery('#test').click(function(){
//console.log('test');
console.log(player);
player.seekTo(25);
return false;
});
});
</script>
<a href="#" id="test">Test</a>
任何想法?谢谢!
推荐答案
这里有更新的jsfiddle 使用新的iframe api工作
Here's an updated jsfiddle using the new iframe api that works
FYI:如果你只是使用iframe纯HTML嵌入你可以放?start = 30
开始时间
FYI: If you're just using an iframe pure HTML embed you can put ?start=30
for the start time
<iframe width="640" height="390"
src="//www.youtube.com/embed/p2H5YVfZVFw?start=30"
frameborder="0" allowfullscreen></iframe>
对于API,您可以在特定时间开始播放视频。
使用 start
参数
For the API you can start a video at a certain time like this.
Use the start
parameter
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'p2H5YVfZVFw',
playerVars: { 'start': 159, 'autoplay': 1, 'controls': 1 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
}
});
您只能在播放器后拨打 seekTo
已经开始比赛或者什么也没做。检查 playerStateChange
回调:
You can only call seekTo
after the player has started playing or it doesn't do anything. Check the playerStateChange
callback :
onStateChange': onPlayerStateChange
添加此回调函数
function onPlayerStateChange(evt)
{
if (evt.data==1)
{
// this will seek to a certain point when video starts
// but you're better off using 'start' parameter
// player.seekTo(22);
}
}
jsFiddle 底部有按钮,可以寻找30,60,90秒。它已经与所有与'罗马'押韵的浏览器进行了测试。当它打开时,它会为 player.seekTo
函数类型设置一个警告框。如果显示未定义,则表示存在问题。
The jsFiddle has buttons at the bottom to seek to 30, 60, 90 seconds. It has been tested with all browsers that rhyme with 'rome'. When it opens it puts up an alert box for the player.seekTo
function type. If this shows 'undefined' you have a problem.
这篇关于YouTube iFrame API .seekTo()不是一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!