模拟鼠标点击Android KitKat上的Youtube视频 [英] Simulate mouse click on Youtube videos on Android Kitkat
问题描述
YouTube允许用户嵌入创建的播放列表与iframe的code的帮助。我想将它们嵌入在网页中播放在Android(奇巧4.4)电视盒硬件。然而,它要求用户首先点击视频。
Youtube allows embedding user created playlist with the help of iframe code. I would like to embed them in a webpage to be played back on Android (Kitkat 4.4) tv box hardware. However it requires user to first click on the video.
因为我发现,苹果iOS和Android禁用大多数移动平台的自动播放功能,避免不良的用户体验。
As I found out that Apple iOS and Android disables autoplay on most mobile platforms to avoid poor user experiences
不过,是有可能在模拟使用Jquery或纯JS溶液(preferred)的iframe用户点击。事情是这样的:
However, is it possible to simulate a user click on the iframe using Jquery or pure JS solution (preferred). Something like this:
function myFunction() {
setTimeout(function() { ("#myiframe").triggerHandler('click'); },3000)
};
我会非常感激,如果有人可以帮助我,因为这功能对于我的目的至关重要的,我已经广泛搜查,但未能得到妥善解决。
I'd be very grateful if somebody can help me with this as this functionality is crucial for my purpose and I have searched extensively but couldn't get a proper solution.
谢谢
DKJ
thanks dkj
推荐答案
早上好dkjain,
据我在 YouTube播放器API文档读过,有几个事件一旦玩家被加载触发,应使用这些事件之一是 onReady()
事件。
as far as I've read in the Youtube Player API docs, there are several events triggered as soon as the Player is loaded, one of these events that should be used is the onReady()
event.
下面是关于如何自动播放一个视频或播放列表更新某些code:
Here's some updated code on how to auto-play a single video or a playlist:
HTML
< DIV ID =玩家/>
将成为YouTube播放器中的占位符
<div id="player"/>
will be the placeholder for the Youtube player
播放播放列表(与 playerVars
):
JS:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
playerVars: {
'listType': 'playlist',
'list': 'PLi5VEqNXXQjVJ4-xZb92wTUawkSQRal0u'
},
events: {
'onReady': onPlayerReady
}
});
}
window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady;
// as soon as the player is loaded, start the playback
function onPlayerReady(event) {
event.target.playVideo();
}
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
document.head.appendChild(tag);
还有一个将autoPlay:1
playerVar,但这似乎并没有可移动plaforms:的https://developers.google.com/youtube/iframe_api_reference#Mobile_considerations
There is also an autoPlay: 1
playerVar, but this does not seem to be available on mobile plaforms: https://developers.google.com/youtube/iframe_api_reference#Mobile_considerations
播放一个视频
JS:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'nhX4rQ79C1A',
events: {
'onReady': onPlayerReady
}
});
}
window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady;
// as soon as the player is loaded, start the playback
function onPlayerReady(event) {
event.target.playVideo();
}
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
document.head.appendChild(tag);
JSFiddles显示上述code:
JSFiddles showing the above code:
单视频: http://jsfiddle.net/Moonbird_IT/akjpmvpf/
播放列表: http://jsfiddle.net/Moonbird_IT/akjpmvpf/4/ (再次更新)
Playlist: http://jsfiddle.net/Moonbird_IT/akjpmvpf/4/ (updated again)
让我知道,如果我们正在朝着正确的方向前进: - )
Let me know if we are going in the right direction :-)
这篇关于模拟鼠标点击Android KitKat上的Youtube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!