当我删除静音时,为什么我的自动播放停止工作



我的代码有问题。我准备在后台运行YouTube视频的代码。但是,当我在我的onplayerready((函数中使用method mute((时,一切都是okey,但是当我删除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();
});

Safari上的自动播放仅允许静音,在下一个版本中对于Chrome也是如此,

自动播放视频静音,并与任何用户交互作用取消静音

最新更新