-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closes issue #222. Autoplay of suggested next video. Button toggle. Does not play duplicates by maintaining history of all videos. #231
Changes from all commits
4549f7b
916dd50
1026b55
cde5acf
3e30725
3818069
503f727
67c9314
da3179a
ca43a82
eae6815
370c3a3
98aca5e
cfe7c6b
21912a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,11 @@ var plyrPlayer; | |
var youTubeDataApiKey = "AIzaSyCxVxsC5k46b8I-CLXlF3cZHjpiqP_myVk"; | ||
var currentVideoID; | ||
|
||
// global playlist, this is populated with an ajax call | ||
var playList = []; | ||
var autoplayState = false; | ||
var videoMetaData = {}; | ||
|
||
var errorMessage = { | ||
init: function() { | ||
// nothing for now | ||
|
@@ -177,6 +182,7 @@ var ZenPlayer = { | |
that.setupTitle(); | ||
that.setupVideoDescription(videoID); | ||
that.setupPlyrToggle(); | ||
that.setupAutoplayToggle(); | ||
}); | ||
|
||
plyrPlayer.addEventListener("playing", function() { | ||
|
@@ -211,6 +217,30 @@ var ZenPlayer = { | |
that.show(); | ||
updateTweetMessage(); | ||
}); | ||
// when player has finished playing | ||
plyrPlayer.addEventListener("ended", function() { | ||
videoMetaData = getParsedVideoMetaData(); | ||
if (videoMetaData) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if this video is already in |
||
videoMetaData["items"].push({"type" :"youtube", "id" :currentVideoID, "played" :true }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect to see a space after the |
||
videoMetaData["autoplayState"] = autoplayState; | ||
} | ||
else { | ||
videoMetaData = { | ||
"autoplayState": autoplayState, | ||
"items": [{ | ||
"type": "youtube", | ||
"id": currentVideoID, | ||
"played": true | ||
}] | ||
}; | ||
} | ||
window.sessionStorage.setItem("videoMetaData", JSON.stringify(videoMetaData)); | ||
if (autoplayState) { | ||
var newId = getNewVideoID(); | ||
that.playNext(newId); | ||
} | ||
}); | ||
|
||
|
||
plyrPlayer.addEventListener("timeupdate", function() { | ||
// Store the current time of the video. | ||
|
@@ -258,6 +288,12 @@ var ZenPlayer = { | |
}); | ||
} | ||
}, | ||
// play next song from autoplay | ||
playNext:function(videoID) { | ||
$("#v").attr("value", videoID); | ||
$("#form").submit(); | ||
}, | ||
|
||
show: function() { | ||
$("#audioplayer").show(); | ||
}, | ||
|
@@ -289,6 +325,31 @@ var ZenPlayer = { | |
toggleElement(event, ".plyr__video-wrapper", "Player"); | ||
}); | ||
}, | ||
setupAutoplayToggle: function() { | ||
// toggle auto next song playing | ||
$("#toggleAutoplay").click(function(event) { | ||
var toggleTextElement = $("#" + event.currentTarget.id); | ||
if (!autoplayState) { | ||
autoplayState = true; | ||
toggleTextElement.text("Stop autoplay"); | ||
} | ||
else { | ||
if (autoplayState === true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't need to compare a truthy value to |
||
toggleTextElement.text("Start autoplay"); | ||
autoplayState = false; | ||
} | ||
else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When does this else branch ever run? We're already checking for |
||
toggleTextElement.text("Stop autoplay"); | ||
autoplayState = true; | ||
} | ||
} | ||
// need to load videoMetaData, change autoplay toggle and set it back again | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove these commented out lines |
||
/* videoMetaData = window.sessionStorage.getItem("videoMetaData"); | ||
videoMetaData = JSON.parse(videoMetaData); | ||
videoMetaData["autoplayState"] = autoplayState; | ||
window.sessionStorage.setItem("videoMetaData", JSON.stringify(videoMetaData));*/ | ||
}); | ||
}, | ||
getVideoDescription: function(videoID) { | ||
var description = ""; | ||
|
||
|
@@ -501,6 +562,44 @@ function pickDemo() { | |
return demos[Math.floor(Math.random() * demos.length)]; | ||
} | ||
|
||
function updateAutoplayToggle(state) { | ||
if (state === true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$("#toggleAutoplay").text("Stop autoplay"); | ||
} | ||
else { | ||
$("#toggleAutoplay").text("Start autoplay"); | ||
} | ||
} | ||
|
||
function getParsedVideoMetaData() { | ||
videoMetaData = window.sessionStorage.getItem("videoMetaData"); | ||
if (videoMetaData) { | ||
videoMetaData = JSON.parse(videoMetaData); | ||
} | ||
return videoMetaData; | ||
} | ||
|
||
function getNewVideoID() { | ||
/* | ||
videoMetaData contains two variables, an autoplay state and information (list) about all videos played till now. | ||
It is stored in session storage, and retrieved when relevant entries need to be added/modified. | ||
*/ | ||
// if playList songs left | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment isn't useful |
||
var nextID = null; | ||
while (playList.length > 0) { | ||
nextID = playList.pop(); | ||
for (var i = 0; i < videoMetaData["items"].length && nextID; i++) { | ||
var curVideo = videoMetaData["items"][i]; | ||
if (curVideo["id"] === nextID) { | ||
// restart the for loop | ||
nextID = playList.pop(); | ||
i = -1; | ||
} | ||
} | ||
} | ||
return nextID; | ||
} | ||
|
||
$(function() { | ||
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { | ||
$("#container").hide(); | ||
|
@@ -518,11 +617,42 @@ $(function() { | |
|
||
errorMessage.init(); | ||
|
||
videoMetaData = getParsedVideoMetaData(); | ||
|
||
if (videoMetaData) { | ||
autoplayState = videoMetaData["autoplayState"]; | ||
} | ||
if (autoplayState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this |
||
updateAutoplayToggle(autoplayState); | ||
} | ||
|
||
// How do we know if the value is truly invalid? | ||
// Preload the form from the URL | ||
var currentVideoID = getCurrentVideoID(); | ||
if (currentVideoID) { | ||
$("#v").attr("value", currentVideoID); | ||
// get similar videos, populate playList | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. crazy indents here |
||
if (!isFileProtocol()) { | ||
$.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++ ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: extra space after |
||
playList.push(data.items[i].id.videoId); | ||
} | ||
} | ||
}).fail(function(jqXHR, textStatus, errorThrown) { | ||
logError(jqXHR, textStatus, errorThrown, "Related video lookup error"); | ||
}); | ||
} | ||
} | ||
else { | ||
var currentSearchQuery = getCurrentSearchQuery(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super nit: Metadata is 1 word, I'd like to see
videoMetadata
everywhere. Don't feel obligated to do this one