Youtube iframe api auto play not working for chrome and video resolution set not working for all browser

天大地大妈咪最大 提交于 2019-12-24 17:24:20

问题


<script>
    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('youtube-vid', { 
                    videoId: 'M7lc1UVf-VE',
                    events: {
                        'onReady': onPlayerReady,
                        'onStateChange': onPlayerStateChange
                    }
                    });
                }
                function onPlayerReady(event) {
                   event.target.playVideo(); 
                    event.target.setPlaybackQuality('hd1080'); 
                }
                function onPlayerStateChange(e) {
                    if (e.data === YT.PlayerState.ENDED) {
                        player.playVideo(); 
                      }
                      
                }
</script>

Please help me out to set youtube quality hd1080 and auto play on page load I used the above code but it not working for me

Thanks in advance!


回答1:


Chrome's autoplay policies changed in April of 2018 https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

Google Chrome will not allow autoplay video. If you want to auto play then you have to mute video. As per policy guidelines.

FireFox browser Google Chrome

// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

//    This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'M7lc1UVf-VE',
        events: {
            'onReady': onPlayerReady
        }
    });
}

//  The API will call this function when the video player is ready.
function onPlayerReady(event) {
    player.setPlaybackRate(2);
    event.target.playVideo();
}
<div id="player"></div>


来源:https://stackoverflow.com/questions/50507985/youtube-iframe-api-auto-play-not-working-for-chrome-and-video-resolution-set-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!