Skip to content
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

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ <h3>
<button class="btn" id="toggleDescription">
Show Description
</button>
<button class="btn" id="toggleAutoplay">
Autoplay
</button>
</div>

<!-- Plyr controls' icons and Plyr player itself -->
Expand Down
130 changes: 130 additions & 0 deletions js/everything.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Copy link
Member

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


var errorMessage = {
init: function() {
// nothing for now
Expand Down Expand Up @@ -177,6 +182,7 @@ var ZenPlayer = {
that.setupTitle();
that.setupVideoDescription(videoID);
that.setupPlyrToggle();
that.setupAutoplayToggle();
});

plyrPlayer.addEventListener("playing", function() {
Expand Down Expand Up @@ -211,6 +217,30 @@ var ZenPlayer = {
that.show();
updateTweetMessage();
});
// when player has finished playing
plyrPlayer.addEventListener("ended", function() {
videoMetaData = getParsedVideoMetaData();
if (videoMetaData) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this video is already in items? We should update it instead of adding a duplicate entry

videoMetaData["items"].push({"type" :"youtube", "id" :currentVideoID, "played" :true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect to see a space after the :, not before. Also, there's an extra space after true

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.
Expand Down Expand Up @@ -258,6 +288,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 @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to compare a truthy value to true, it can just be if (autoplayState) {

toggleTextElement.text("Start autoplay");
autoplayState = false;
}
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does this else branch ever run? We're already checking for !autoplayState in the first if branch. Looks like we can simplify this to a single if/else

toggleTextElement.text("Stop autoplay");
autoplayState = true;
}
}
// need to load videoMetaData, change autoplay toggle and set it back again
Copy link
Member

Choose a reason for hiding this comment

The 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 = "";

Expand Down Expand Up @@ -501,6 +562,44 @@ function pickDemo() {
return demos[Math.floor(Math.random() * demos.length)];
}

function updateAutoplayToggle(state) {
if (state === true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (state) {

$("#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
Copy link
Member

Choose a reason for hiding this comment

The 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();
Expand All @@ -518,11 +617,42 @@ $(function() {

errorMessage.init();

videoMetaData = getParsedVideoMetaData();

if (videoMetaData) {
autoplayState = videoMetaData["autoplayState"];
}
if (autoplayState) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this if is needed.

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
Copy link
Member

Choose a reason for hiding this comment

The 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++ ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space after i++

playList.push(data.items[i].id.videoId);
}
}
}).fail(function(jqXHR, textStatus, errorThrown) {
logError(jqXHR, textStatus, errorThrown, "Related video lookup error");
});
}
}
else {
var currentSearchQuery = getCurrentSearchQuery();
Expand Down