问题描述
我已经进行了大量搜索,但到目前为止还没有找到解决方案...
I've done plenty of searching but can't find a olsution so far...
我有一个 Youtube 播放列表.我想把它嵌入一个网站.但不是总是从第一个视频开始,我希望它从一个随机视频开始,或者至少是一个特定视频(我可以使用服务器端代码随机选择).
I have a Youtube playlist. I want to embed it on a website. But instead of always starting at the first video, I want it to start at either a random video, or a at least a specific video (which I can randomly select with server side code).
我查看了 iframe 嵌入 URL 的选项,但没有看到允许我对此进行任何控制的参数.可以吗?
I have looked at the options for the iframe embed URL and can't see a parameter that allows me any control over this. Is it possible to do?
否则我认为我需要使用 API 在 JavaScript 中做一些事情.有人可以粘贴或指向我一些示例代码,我可以用来完成此操作,因为我以前没有使用过 Youtube API?
Otherwise I presume I would need to do something in JavaScript with the API. Can someone paste or point me to some example code that I could use to accomplish this as I haven't used the Youtube API before?
推荐答案
在您的服务器上生成一个随机数,然后在 url 中使用 index 参数.这是一个示例,其中播放列表将通过添加 index=7 从第 7 个项目开始.
Generate a random number on your server and then use the index parameter in the url. Here's an example where the playlist will start on the 7th item by adding index=7.
<iframe width="560" height="315" src="http://www.youtube.com/embed/videoseries?list=PL9C5815B418D1508E&index=7" frameborder="0" allowfullscreen></iframe>
另一种选择是使用 js api,并调用 loadPlaylist 并包含 index 参数.https://developers.google.com/youtube/js_api_reference#loadPlaylist
The other option is to use the js api, and call loadPlaylist and include the index argument.https://developers.google.com/youtube/js_api_reference#loadPlaylist
**自从 Google 引入了 YouTube 的 3.0 API,index
参数现在从零开始.因此,如果我们想从第 7 个视频开始,我们将使用 &index=6
而不是 &index=7
修改上面的示例.
** Since Google introduced YouTube's 3.0 API, the index
parameter is now zero-based. Therefore, if we wanted to start at the 7th video, we'd modify the example above using &index=6
instead of &index=7
.
(链接:https://developers.google.com/youtube/iframe_api_reference#Queueing_Functions_for_Playlists)
这篇关于嵌入 Youtube 播放列表但从随机视频开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!