如何在本地主机的html文件中使用YouTube IFRAME-Player

How to use YouTube IFRAME-Player in localhost html file?

本文关键字:文件 IFRAME-Player YouTube html 主机      更新时间:2023-09-26

我正在编写一个仅html的应用程序,并希望嵌入一个基于YouTube iframe的播放器。我使用了来自https://developers.google.com/youtube/iframe_api_reference的演示代码,不幸的是,对于通过file://协议加载的HTML文件,这似乎不能开箱即用。我得到以下错误:

www-widgetapi.js: 100

在'DOMWindow'上执行'postMessage'失败:提供的目标来源('file://')与接收窗口的来源('null')不匹配。

I found Failed to execute '在'DOMWindow': https://www.youtube.com !== http://localhost:9000不幸的是,它没有帮助:我是否包括它使用他们的JS文件通过http或https并不重要。

如果你不使用自己的IFRAME,而是让YouTube API创建IFRAME ->没有任何问题。不知道为什么,所以如果有人能详细说明我很感兴趣。

编辑,一些示例代码,虽然它是几年前的:

let player = new YT.Player('gv-player-iframe', {
          height: playerHeight,
          width: playerWidth,
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange,
            'onError': onPlayerError
          }
        });

这里的问题是,您正在通过文件系统运行它。

更多信息请阅读