问题
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?
ELABORATING ON WHAT I MEAN:
In the following snippet of code:
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)
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.
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.
回答1:
You probably want to just use player.getPlaylistIndex()
https://developers.google.com/youtube/iframe_api_reference#Retrieving_video_information
来源:https://stackoverflow.com/questions/37724246/get-youtube-playlist-index