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中的safari上不起作用,但在ios中没有提到chrome。请帮我找到解决方案。非常感谢。
因为这不是浏览器问题。这是关于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.
请在官方网站上查看有关这些限制的完整列表。
不,你不能从IFrame api:
移动注意事项自动播放和脚本播放HTML5元素,仅在某些移动浏览器(如Chrome和Safari)中允许在由用户交互启动的情况下进行回放(例如敲击播放器)。以下是苹果的文档:
"警告:要防止通过蜂窝网络主动下载,请访问由用户承担费用,嵌入式媒体无法在iOS上的Safari——用户总是启动播放。">
由于这种限制,playVideo()、loadVideoById()不能在所有移动环境中工作。
来自YouTube IFrame Player API官方文档
结论->不,你不能