YouTube iFrame API .seekTo() not a method?(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 = "aHR0cDovL3d3dy55b3V0dWJlLmNvbS9wbGF5ZXJfYXBp";
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="Ly93d3cueW91dHViZS5jb20vZW1iZWQvcDJINVlWZlpWRnc/c3RhcnQ9MzA="
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.
这篇关于YouTube iFrame API .seekTo() 不是方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:YouTube iFrame API .seekTo() 不是方法?
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01