YouTube嵌入:自动播放1080p?

时间:2014-12-21 06:25:16

标签: php html youtube youtube-api

您以前能够do this获取嵌入式YouTube视频以自动播放1080p(或其他品质),但是,这不再适用。

是否有另一种最新的方法来实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可以使用此处的iframe API:

https://developers.google.com/youtube/iframe_api_reference

使用API​​,您可以在嵌入视频准备就绪后指定播放质量,如下例所示:

<div id="player"></div>

<script>
    // 2. 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);

    // 3. 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: 'INSERT_VIDEO_ID_HERE',
            events: {
                'onReady': onPlayerReady
            }
        });
    }

    // 4. The API will call this function when the video player is ready.
    // In this case we set the playback quality to 1080p
    function onPlayerReady(event) {
        event.target.setPlaybackQuality('hd1080')
    }
</script>