Skip to content

Commit

Permalink
Default autoplay with basic history tracking to avoid duplicate songs…
Browse files Browse the repository at this point in the history
… play. For issue zen-audio-player#222
  • Loading branch information
cipher1729 committed Jul 28, 2016
1 parent d729019 commit 2b50ea2
Showing 1 changed file with 89 additions and 3 deletions.
92 changes: 89 additions & 3 deletions js/everything.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ var plyrPlayer;
var youTubeDataApiKey = "AIzaSyCxVxsC5k46b8I-CLXlF3cZHjpiqP_myVk";
var currentVideoID;

// global playlist, this is populated with an ajax call
var playList = [];




var errorMessage = {
init: function() {
// nothing for now
Expand Down Expand Up @@ -123,7 +129,6 @@ function handleYouTubeError(details) {
sendKeenEvent("YouTube iframe API error", {verbose: verboseMessage, message: message, code: details.code});

// Log debug info
console.log("Verbose debug error message: ", verboseMessage);
}
}

Expand Down Expand Up @@ -206,11 +211,63 @@ var ZenPlayer = {
seconds: plyrPlayer.plyr.embed.getDuration(),
youtubeID: plyrPlayer.plyr.embed.getVideoData().video_id
});

// Show player
that.show();
updateTweetMessage();
});
// when player has finished playing
plyrPlayer.addEventListener("ended", function() {
plyrPlayer.removeEventListener("ended");
/*
idMap is dict- history of all songs played till now, of the form
<key=videoId>: <value=true>. It is stored in the session storage of the
browser. When the player has finished playing, the last entry from the playlist
is picked as a possible candidate and checked for presence in idMap. If its present ie its
already been played, we pop() and get the new last entry from playlist, looking for a new song. Whatever
is a valid new entry gets added to the dict which is then written to session storage again
*/
// if playList songs left
if (playList.length !== 0)
{
// get autoplay candidate from playlist
var newId = playList[playList.length - 1];
// load 'history' dict from storage
var idMap = window.sessionStorage.getItem("idMap");
idMap = JSON.parse(idMap);
// first time?
if (idMap !== null && newId !== "")
{
// keep popping till unique next song found
while ( (newId in idMap) && (playList.length !== 0))
{
playList.pop();
if (playList.length >= 1)
{
newId = playList[playList.length - 1];
}
}
// nothing to be played if empty playlist
if (playList.length === 0)
{
newId = null;
}
else {
// write 'history' dict to storage
idMap[newId] = true;
window.sessionStorage.setItem("idMap", JSON.stringify(idMap));
}
}
else {
idMap = {};
idMap[newId] = true;
window.sessionStorage.setItem("idMap", JSON.stringify(idMap));
}
// play the new song from autoplay
that.playNext(newId);
}

});


plyrPlayer.addEventListener("timeupdate", function() {
// Store the current time of the video.
Expand Down Expand Up @@ -250,6 +307,12 @@ var ZenPlayer = {
});
}
},
// play next song from autoplay
playNext:function(videoID) {
$("#v").attr("value", videoID);
$("#form").submit();
},

show: function() {
$("#audioplayer").show();
},
Expand Down Expand Up @@ -284,7 +347,6 @@ var ZenPlayer = {
var description = "";

if (isFileProtocol()) {
console.log("Skipping video description request as we're running the site locally.");
$("#toggleDescription").hide();
}
else {
Expand Down Expand Up @@ -479,6 +541,30 @@ $(function() {
var currentVideoID = getCurrentVideoID();
if (currentVideoID) {
$("#v").attr("value", currentVideoID);
// get similar videos, populate playList
$.ajax({
url: "https://www.googleapis.com/youtube/v3/search",
dataType: "json",
async: false,
data: {
key: youTubeDataApiKey,
part: "snippet",
type: "video",
relatedToVideoId: currentVideoID
},
success: function(data) {
// push items into playlist
for (var i = 0;i < data.items.length;i ++ )
{
playList.push(data.items[i].id.videoId);
}
}
}).fail(function(jqXHR, textStatus, errorThrown) {
logError(jqXHR, textStatus, errorThrown, "Lookup error");
});



}
else {
var currentSearchQuery = getCurrentSearchQuery();
Expand Down

0 comments on commit 2b50ea2

Please sign in to comment.