YouTube iFrame API .seekTo() 不是一种方法?
我知道 .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
仅供参考:如果您只是使用 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 秒.它已经在所有与 'rome' 押韵的浏览器上进行了测试.当它打开时,它会为 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.
相关文章