嵌入的 YouTube 视频在本地服务器上不起作用

2022-01-19 00:00:00 youtube youtube-api iframe javascript html

我在 StackOverflow 上看到了很多这样的问题,但没有一个能给出有效的答案.我的问题是我无法嵌入 YouTube 视频,因为此视频不可用".出现:

I saw many questions like this asked on StackOverflow, but none of them had a working answer. My problem is that I can't embed a YouTube video, because a "This video is not available." comes up:

这是我迄今为止所知道和尝试过的:

Here's what I know and have tried so far:

  • 并非所有视频都会出现此错误.据我所知,它大多不适用于流行音乐视频.
  • 在本地 HTML 文件 (file:///C:/path/to/file.html) 中嵌入这些 YouTube 视频不起作用,当嵌入的视频在我的本地 Django 上时也不起作用服务器(12.345.67.89:8080/page).
  • 它不适用于 Youtube 的
  • 我尝试将 /embed//watch?v= 作为 URL.
  • 使用 iframe 时,将 https:// 更改为 http://// 不起作用.去掉 www. 部分也不行.
  • 有趣的是,当我将代码放入 JSFiddle 时,它确实有效.
  • 它不适用于 Chrome 和 Firefox.
  • 控制台中没有错误.
  • 从我的手机(未通过我的 WiFi 连接)或网络代理访问网站也不起作用.
  • This error doesn't come up on all videos. From what I've noticed, it's mostly not working on popular music videos.
  • Embedding these YouTube videos doesn't work in a local HTML file (file:///C:/path/to/file.html) nor when the embedded video is on my local Django server (12.345.67.89:8080/page).
  • It doesn't work both with Youtube's
  • I tried both /embed/ and /watch?v= as the URL.
  • When using the iframe, changing https:// to http:// or // doesn't work. Taking the www. part out doesn't work either.
  • Interestingly, it does work when I put the code in JSFiddle.
  • It doesn't work on both Chrome and Firefox.
  • There are no errors in the console.
  • Accessing the website from my phone (not connecting from my WiFi) or a web proxy doesn't work either.

推荐答案

Sergiy Shcherbina 的回答帮我解决了这个问题.我正在从 vm 运行我的 Web 服务器并通过我的本地 IP 地址访问它.

Sergiy Shcherbina's answer helped me solve this issue. I was running my web server from a vm and accessing it through my local IP address.

您似乎需要通过主机名而不是 IP 地址来访问包含嵌入式 youtube 播放器的页面.

It seems that you need to access your page that contains the embedded youtube player through a host name and NOT an IP address.

我如何为我的开发环境解决这个问题

在更改之前,我是这样访问我的网页的:http://192.168.xx:3000/video 并且对于许多可验证的视频,我会收到视频不可用"错误可嵌入".

Before the change, I was accessing my web-page like this: http://192.168.x.x:3000/video and i would get 'video unavailable' errors for many videos that were verifiably 'embeddable'.

Hacky word-around:我在 hosts 文件中为我的虚拟机设置了一个主机名,并将我的 IP 地址别名为 dev-vm.如何编辑您的主机文件

A Hacky word-around: I set a hostname for my VM in my hosts file and aliased my IP address to dev-vm. How to edit your host file

现在我访问包含我的嵌入式播放器的页面,如下所示:http://dev-vm:3000/video 现在正在播放以前给我视频不可用"错误的视频.

Now i access the page which has my embedded player like this: http://dev-vm:3000/video and now videos that previously gave me a 'video unavailable' error are now playing.

多么奇怪的问题.我希望这可以帮助一些遇到类似问题的人.

What a bizarre issue. I hope this helps some people who are having a similar issue.

相关文章