php 如何在多个客户端上同步播放 YouTube 视频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8423091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:40:14  来源:igfitidea点击:

How to play YouTube videos in sync on multiple clients?

phpjqueryajaxyoutubereal-time

提问by Anthony

I'm looking to understand how to build a web app that allows multiple users to view the same YouTube video in sync on their computers. For example, synchtube.comor watch2gether.com

我想了解如何构建一个网络应用程序,允许多个用户在他们的计算机上同步查看相同的 YouTube 视频。例如,synchtube.comwatch2gether.com

I'm seeking a simple approach that can be implemented using common technologies such as PHP, jQueryand Apache. I'm currently thinking along the lines of polling (or comet long polling but that requires more complex server setup which I hope to avoid), similar to how Ajaxchat apps are implemented - that is clients continuously check with the server to see which video is playing. However, I suspect there will be latency issues here so videos will not be completely in sync. I can't think of a better approach yet.

我正在寻找一种可以使用通用技术(例如PHPjQuery和 )来实现的简单方法Apache。我目前正在考虑轮询(或彗星长轮询,但需要更复杂的服务器设置,我希望避免),类似于Ajax聊天应用程序的实现方式 - 即客户端不断检查服务器以查看哪个视频是玩。但是,我怀疑这里会有延迟问题,因此视频不会完全同步。我还想不出更好的方法。

To fellow developer community, any help on the methodology or code snippets are hugely appreciated!

对于开发人员社区,非常感谢对方法论或代码片段的任何帮助!

回答by leggetter

Here's an example of keeping videos in sync using WebSockets and node.js: http://softwareas.com/video-sync-with-websocket-and-node

这是使用 WebSockets 和 node.js 保持视频同步的示例:http://softwareas.com/video-sync-with-websocket-and-node

I know you don't want to use this tech, it's just a good starting point to show what you can do and how.

我知道您不想使用这项技术,这只是展示您可以做什么以及如何做的一个很好的起点。

Since you want to use PHP and keep things simple then your best solution is to outsource the realtime communications layer (used to keep the user videos in sync) to a hosted service such as Pusher, who I work for. This means you don't need to maintain the persistent connection and you can use Pusher to deliver realtime events about the video states between users/clients.

由于您想使用 PHP 并保持简单,因此您最好的解决方案是将实时通信层(用于保持用户视频同步)外包给我为之工作的诸如Pusher 之类的托管服务。这意味着您不需要维护持久连接,并且您可以使用 Pusher 在用户/客户端之间传递有关视频状态的实时事件。

If you want to use YouTube videos, and HTML5 isn't always going to be available, then you'll need to use the Player API. I can't see a similar timeupdatedevent (available in HTML5 video) so you might need to periodically check the video time (see player status API section. You can register for state change events so you know when a user pauses, stops a video etc.

如果您想使用 YouTube 视频,并且 HTML5 并不总是可用,那么您需要使用Player API。我看不到类似的timeupdated事件(在 HTML5 视频中可用),因此您可能需要定期检查视频时间(请参阅播放器状态 API 部分。您可以注册状态更改事件,以便知道用户何时暂停、停止视频等) .

@rob-w's comment provides some good insight into using the YouTube API.

@rob-w 的评论为使用 YouTube API 提供了一些很好的见解。

Note: This might actually make for a really interesting blog post so if you are interested let me know and I'll see what I can do.

注意:这实际上可能会成为一篇非常有趣的博客文章,所以如果你有兴趣让我知道,我会看看我能做些什么。

回答by AlexB

You have a couple of ways you can accomplish this:

您有几种方法可以实现这一点:

  • The ideal solution is to use a real time collaboration protocol and server (such as XMPPand OpenFire) set-up and develop a custom module that will allow you to pass custom stanzas back and forth. Unfortunately not only it doesn't meet your requirements about the tech involved but it's also rather complicated to implement, but the result is spectacular.

  • Next up, if cross-browser compatibility is not an issue, you can use the WebSocket API in HTML 5. Check out this guy's exampleon how to build an HTML 5 WebSocket-enable chat.

  • Finally, you're left with AJAX polling, either the usual method or long-polling, in which you block the server side script execution until you have some data available. See this linkfor further reference.

  • 理想的解决方案是使用实时协作协议和服务器(例如XMPPOpenFire)设置并开发一个自定义模块,允许您来回传递自定义节。不幸的是,它不仅不能满足您对所涉及技术的要求,而且实施起来也相当复杂,但结果却是惊人的。

  • 接下来,如果跨浏览器兼容性不是问题,您可以使用 HTML 5 中的 WebSocket API。查看此人的示例,了解如何构建支持 HTML 5 WebSocket 的聊天。

  • 最后,剩下的就是 AJAX 轮询,无论是通常的方法还是长轮询,在这种轮询中,您会阻止服务器端脚本的执行,直到您有一些数据可用。请参阅此链接以获取进一步参考。

回答by florine

For me the ideal solution is to use Vynch. With this technology you can embedded in your own website a room with video that you can watch synchronized. You can create playlist and share with friends and user! there is also a chat to exchange your impressions..

对我来说,理想的解决方案是使用 Vynch。使用这项技术,您可以在自己的网站中嵌入一个可以同步观看的视频房间。您可以创建播放列表并与朋友和用户分享!还有一个聊天来交流你的印象..