问题描述
我使用 Youtube API 自动播放视频,但我发现它只能在 PC 上正常运行,而不能在移动设备上运行.我做了一些 google-ing,发现在移动设备上禁用了自动播放功能.
I have used Youtube API to play automatically a video but i have found out that it only works fine on PCs but NOT mobile devices. I did a bit of google-ing and discovered that autoplay feature is disabled on mobile devices.
不,问题是,我可以检测自动播放是否被禁用??.或者至少在视频处于状态 -1 时强制视频显示 Youtoube 播放按钮.
No the questions is, can i detect if autoplay is disabled ??. Or at least force the video to show youtoube play button if vidio is on state -1.
即
if(event.data == -1) {
// show play button
}
推荐答案
不使用 arg 自动播放的示例 autoplay
.我只在 PC 而非移动设备上测试此解决方案,但您可以尝试此代码:
A sample to autoplay without using arg autoplay
. I only test this solution on PCs not mobile but you can try this code :
HTML
<div id="player"></div>
JS
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
} else {
}
}
function stopVideo() {
player.stopVideo();
}
这篇关于Youtube API 检测自动播放是否被禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!