使用现有的 Iframe 访问 youtube API(在 youtube 视频网站上使用共享)

Access youtube API with exist Iframe (using share on the youtube video website)

如何使用现有的 Iframe 访问 youtube API(在 youtube 视频网站上使用共享)我的代码:
<iframe id="welcome" width="100%" height="100%" src="SomeUrl" frameborder="0" allowfullscreen class="player"></iframe>

你可以访问这个documentation:

Requirements

The user's browser must support the HTML5 postMessage feature. Most modern browsers support postMessage, though Internet Explorer 7 does not support it.

Embedded players must have a viewport that is at least 200px by 200px. If the player displays controls, it must be large enough to fully display the controls without shrinking the viewport below the minimum size. We recommend 16:9 players be at least 480 pixels wide and 270 pixels tall.

Any web page that uses the IFrame API must also implement the following JavaScript function:

  • onYouTubeIframeAPIReady – The API will call this function when the page has finished downloading the JavaScript for the player API, which enables you to then use the API on your page. Thus, this function might create the player objects that you want to display when the page loads.

Examples也给了下手

这个SO post也可以作为参考

这里的JavaScript有效。

var player;
function onYouTubeIframeAPIReady() {
    video = document.getElementById('video0');
    player = new YT.Player(video, {
        events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
            }
        })
    }
function onPlayerReady(e) {
    console.log("hey Im ready");
    }
function onPlayerStateChange(e) {
    console.log(e)
    }