我的代码有问题。我准备了在后台运行YouTube视频的代码。但是当我在我的onPlayerReady()函数中使用方法mute()时,一切都很好,但是当我删除静音()时,我在Safari上的视频自动播放不起作用。你有什么问题吗?我需要带声音的视频。
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/player_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var tv,
playerDefaults = {autoplay: 1, autohide: 1, modestbranding: 0, rel: 0, showinfo: 0, controls: 0, disablekb: 0, enablejsapi: 0, iv_load_policy: 3};
var vid = [
{'videoId': 'PBcQAfxi1WY', 'startSeconds': 0, 'endSeconds': 22, 'suggestedQuality': 'hd1080'},
],
randomVid = Math.floor(Math.random() * vid.length),
currVid = randomVid;
$('.hi em:last-of-type').html(vid.length);
function onYouTubePlayerAPIReady(){
tv = new YT.Player('tv', {events: {'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange}, playerVars: playerDefaults});
}
function onPlayerReady(){
tv.loadVideoById(vid[currVid]);
tv.mute();
}
function onPlayerStateChange(e) {
if (e.data === 1){
$('#tv').addClass('active');
$('.hi em:nth-of-type(2)').html(currVid + 1);
} else if (e.data === 2){
$('#tv').removeClass('active');
if(currVid === vid.length - 1){
currVid = 0;
} else {
currVid++;
}
tv.loadVideoById(vid[currVid]);
tv.seekTo(vid[currVid].startSeconds);
}
}
function vidRescale(){
var w = $(window).width(),
h = $(window).height();
if (w/h > 16/9){
tv.setSize(w, w/16*9);
$('.tv .screen').css({'left': '0px'});
} else {
tv.setSize(h/9*16, h);
$('.tv .screen').css({'left': -($('.tv .screen').outerWidth()-w)/2});
}
}
$(window).on('load resize', function(){
vidRescale();
});
$('.hi span:first-of-type').on('click', function(){
$('#tv').toggleClass('mute');
$('.hi em:first-of-type').toggleClass('hidden');
if($('#tv').hasClass('mute')){
tv.mute();
} else {
tv.unMute();
}
});
$('.hi span:last-of-type').on('click', function(){
$('.hi em:nth-of-type(2)').html('~');
tv.pauseVideo();
});
答案 0 :(得分:2)
safari上的自动播放只允许静音,下一版本中的chrome也是如此,
自动播放视频静音,与任何用户互动取消静音应该可以正常工作