获取 youtube 播放列表索引 [英] get youtube playlist index
问题描述
我使用 youtube API(使用 javascript)在我的网站中嵌入了一个播放列表,并且该播放列表包含 3 首歌曲.
I have a playlist embeded in my website using the youtube API (with javascript) and the playlist has 3 songs.
问题:如何获取当前正在播放的歌曲的索引?
QUESTION: How can I get the index of the song that is currently playing?
详细说明我的意思:
在以下代码片段中:
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
loadPlaylist:{
listType:'playlist',
list:['1asdfasdfs','2asdfasdfs','3asdfasdfs'],
index:parseInt(0),
suggestedQuality:'small'
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
index:parseInt(0)
告诉 youtube api 从列表中的第一首歌曲 (1asdfasdfs
) 开始播放歌曲.我假设该索引在不断变化,因为用户可以单击播放列表中的 3 首歌曲中的任何一首并随时切换到另一首歌曲.我想知道有什么方法可以获取当前索引.
index:parseInt(0)
is telling the youtube api to start playing the song from the first song in the list (1asdfasdfs
). I am assuming that this index is contantly shifting, since the users can click any of the 3 songs in the playlist and shift to another song at any time they want. What I would like is to know what method exists to get the current index.
注意:我意识到我可能有点笨拙,使用 player.getVideoData()['video_id'];
来检索视频 ID,然后循环通过三首歌曲的列表 ['1asdfasdfs','2asdfasdfs','3asdfasdfs']
然后将该视频 ID 的位置保存在数组中,但是我相信有一种更清晰的方法可用,可能youtube api中的方法?不过我好像在任何地方都找不到.
Note: I recognize that I could be a bit hacky and use player.getVideoData()['video_id'];
to retrieve the video id, and then loop through the list of three songs ['1asdfasdfs','2asdfasdfs','3asdfasdfs']
and then save the position of that video id in the array, however I believe there is a cleaner method available, possibly a method in the youtube api? I can't seem to find it anywhere though.
推荐答案
您可能只想使用 player.getPlaylistIndex()
https://developers.google.com/youtube/iframe_api_reference#Retrieving_video_information
这篇关于获取 youtube 播放列表索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!