if($('.explore-video-btn').length > 0) {
var video_id = youtube_parser($('.explore-video-btn').attr('data-video-url'));
}
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
//Holds a reference to the YouTube player
//this function is called by the API
if(document.getElementsByClassName('explore-video-btn').length !== 0) {
document.getElementsByClassName('explore-video-btn')[0].addEventListener('click', function() {
onYouTubePlayerAPIReady();
});
}
var player;
function onYouTubePlayerAPIReady() {
setTimeout(function() {
player = new YT.Player('youtube-iframe', {
videoId: video_id,
playerVars: {'rel': 0, 'showinfo': 0, 'ecver': 2, 'autoplay': 1},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
//subscribe to events
}, 200);
}
function onPlayerReady(event) {
event.target.mute();
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
event.target.stopVideo();
}
}
我已使用youtube API自动播放视频,它在chrome浏览器中的android设备上正常运行,但在chrome浏览器中的iphone设备上无法自动播放。我发现了youtube API文档警告,例如“警告:为防止用户自行付费通过蜂窝网络进行下载,嵌入式媒体无法在iOS的Safari中自动播放-用户始终启动播放。”
警告说自动播放不适用于ios中的野生动物园,但不能提及ios中的Chrome。请帮助我找到解决方案。谢谢。
答案 0 :(得分:1)
因为这不是浏览器问题。这是关于iOS的。
视频政策从iOS开始。
<video autoplay> elements will now honor the autoplay attribute, for elements which meet the following conditions: <video> elements will be allowed to autoplay without a user gesture if their source media contains no audio tracks. <video muted> elements will also be allowed to autoplay without a user gesture. If a <video> element gains an audio track or becomes un-muted without a user gesture, playback will pause. <video autoplay> elements will only begin playing when visible on-screen such as when they are scrolled into the viewport, made visible through CSS, and inserted into the DOM. <video autoplay> elements will pause if they become non-visible, such as by being scrolled out of the viewport.
See a full list about these restrictions on official site.
不,您不能从iframe api中获取
:移动注意事项自动播放和脚本播放HTML5 元素,仅在某些移动浏览器(例如Chrome和Safari)中 如果播放是由用户互动发起的,则允许进行播放 (例如点击播放器)。这是苹果公司的摘录 文档:
“警告:为防止在以下位置通过蜂窝网络进行未经请求的下载: 用户的费用,嵌入式媒体无法自动播放 iOS上的Safari浏览器-用户始终会启动播放。”
由于此限制,因此功能和参数(例如自动播放, playVideo(),loadVideoById()不适用于所有移动环境。
From YouTube IFrame Player API official document
结论->不,你不能