YouTube iframe API: how do I control an iframe player that's already in the HTML?

前端 未结 5 1434
[愿得一人]
[愿得一人] 2020-11-22 08:00

I want to be able to control iframe based YouTube players. This players will be already in the HTML, but I want to control them via the JavaScript API.

I\'ve been re

5条回答
  •  爱一瞬间的悲伤
    2020-11-22 08:16

    My own version of Kim T's code above which combines with some jQuery and allows for targeting of specific iframes.

    $(function() {
        callPlayer($('#iframe')[0], 'unMute');
    });
    
    function callPlayer(iframe, func, args) {
        if ( iframe.src.indexOf('youtube.com/embed') !== -1) {
            iframe.contentWindow.postMessage( JSON.stringify({
                'event': 'command',
                'func': func,
                'args': args || []
            } ), '*');
        }
    }
    

提交回复
热议问题