Skip to content

Commit 5aa2679

Browse files
Added autoplay next related video feature
1 parent e3694e9 commit 5aa2679

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed

css/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ a {
100100
@media all and (min-width: 480px) {
101101

102102
#container {
103-
max-width: 450px;
103+
max-width: 470px;
104104
}
105105

106106
}

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ <h3>
8888
<button class="btn" id="toggleDescription">
8989
Show Description
9090
</button>
91+
<button class="btn" id="toggleAutoplay">
92+
Autoplay
93+
</button>
9194
<button class="btn" id="toggleRepeat">
9295
Repeat Track
9396
</button>

js/everything.js

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ var plyrPlayer;
1515
var youTubeDataApiKey = "AIzaSyCxVxsC5k46b8I-CLXlF3cZHjpiqP_myVk";
1616
var currentVideoID;
1717

18+
// global playlist, this is populated with an ajax call
19+
var tags = [];
20+
var playList = new Set();
21+
var autoplayState = false;
22+
const MAX_TAGS = 10;
23+
1824
var errorMessage = {
1925
init: function() {
2026
// nothing for now
@@ -137,6 +143,7 @@ var ZenPlayer = {
137143
that.setupTitle();
138144
that.setupVideoDescription(videoID);
139145
that.setupPlyrToggle();
146+
that.setupAutoplayToggle();
140147
});
141148

142149
plyrPlayer.addEventListener("playing", function() {
@@ -165,6 +172,17 @@ var ZenPlayer = {
165172
updateTweetMessage();
166173
});
167174

175+
// when player has finished playing
176+
plyrPlayer.addEventListener("ended", function() {
177+
if (autoplayState) {
178+
if (playList.length === 0 || playList.size === 0) {
179+
fetchSuggestedVideoIds();
180+
}
181+
var newId = getNewVideoID();
182+
that.playNext(newId);
183+
}
184+
});
185+
168186
plyrPlayer.addEventListener("timeupdate", function() {
169187
// Nothing is playing
170188
if (!plyrPlayer.plyr || !plyrPlayer.plyr.embed) {
@@ -223,6 +241,11 @@ var ZenPlayer = {
223241
});
224242
}
225243
},
244+
// play next song from autoplay
245+
playNext: function(videoID) {
246+
$("#v").val(videoID);
247+
$("#form").submit();
248+
},
226249
show: function() {
227250
$("#audioplayer").show();
228251
// Hide the demo link as some video is playing
@@ -258,6 +281,24 @@ var ZenPlayer = {
258281
toggleElement(event, ".plyr__video-wrapper", "Player");
259282
});
260283
},
284+
setupAutoplayToggle: function() {
285+
// toggle auto next song playing
286+
$("#toggleAutoplay").click(function(event) {
287+
var toggleTextElement = $("#" + event.currentTarget.id);
288+
if (autoplayState) {
289+
toggleTextElement.text("Start autoplay");
290+
autoplayState = false;
291+
window.sessionStorage.removeItem("autoPlay");
292+
window.sessionStorage.removeItem("playList");
293+
}
294+
else {
295+
toggleTextElement.text("Stop autoplay");
296+
autoplayState = true;
297+
window.sessionStorage.setItem("autoPlay", true);
298+
}
299+
});
300+
},
301+
261302
getVideoDescription: function(videoID) {
262303
var description = "";
263304

@@ -275,6 +316,7 @@ var ZenPlayer = {
275316
}
276317
else {
277318
description = data.items[0].snippet.description;
319+
tags = data.items[0].snippet.tags;
278320
}
279321
},
280322
function(jqXHR, textStatus, errorThrown) {
@@ -524,6 +566,79 @@ function pickDemo() {
524566
return demos[Math.floor(Math.random() * demos.length)];
525567
}
526568

569+
function updateAutoplayToggle(state) {
570+
if (state) {
571+
$("#toggleAutoplay").text("Stop autoplay");
572+
}
573+
else {
574+
$("#toggleAutoplay").text("Start autoplay");
575+
}
576+
}
577+
578+
function getNewVideoID() {
579+
var nextID = null;
580+
nextID = playList.pop();
581+
while (currentVideoID === nextID) {
582+
nextID = playList.pop();
583+
}
584+
window.sessionStorage.setItem("playList", JSON.stringify(playList));
585+
return nextID;
586+
}
587+
588+
function fetchSuggestedVideoIds() {
589+
if (tags.length) {
590+
// get similar videos, populate playList
591+
if (!isFileProtocol()) {
592+
for (let index = 0; index < tags.length && index < MAX_TAGS; index++) {
593+
$.ajax({
594+
url: "https://www.googleapis.com/youtube/v3/search",
595+
dataType: "json",
596+
async: false,
597+
data: {
598+
key: youTubeDataApiKey,
599+
part: "snippet",
600+
type: "video",
601+
order: "relevance",
602+
q: tags[index],
603+
maxResults: 2
604+
},
605+
success: onRelatedVideoFetchSuccess
606+
}).fail(function(jqXHR, textStatus, errorThrown) {
607+
logError(jqXHR, textStatus, errorThrown, "Related video lookup error");
608+
});
609+
}
610+
playList = Array.from(playList);
611+
window.sessionStorage.setItem("playList", JSON.stringify(playList));
612+
}
613+
}
614+
}
615+
616+
function onRelatedVideoFetchSuccess(data) {
617+
// push items into playlist
618+
for (var i = 0; i < data.items.length; i++) {
619+
playList.add(data.items[i].id.videoId);
620+
}
621+
}
622+
623+
function loadAutoPlayDetails() {
624+
// load playList from session storage on reload
625+
if (window.sessionStorage.getItem("playList")) {
626+
playList = JSON.parse(window.sessionStorage.getItem("playList"));
627+
}
628+
629+
// fetch autoPlay from session storage on reload
630+
if (window.sessionStorage.getItem("autoPlay")) {
631+
autoplayState = window.sessionStorage.getItem("autoPlay");
632+
updateAutoplayToggle(autoplayState);
633+
}
634+
}
635+
636+
function resetAutoPlayList() {
637+
playList = new Set();
638+
tags = [];
639+
window.sessionStorage.removeItem("playList");
640+
}
641+
527642
$(function() {
528643
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
529644
$("#container").hide();
@@ -534,6 +649,8 @@ $(function() {
534649

535650
errorMessage.init();
536651

652+
loadAutoPlayDetails();
653+
537654
// How do we know if the value is truly invalid?
538655
// Preload the form from the URL
539656
var currentVideoID = getCurrentVideoID();
@@ -608,20 +725,22 @@ $(function() {
608725
data: {
609726
key: youTubeDataApiKey,
610727
part: "snippet",
611-
fields: "items/snippet/description",
612728
id: videoID
613729
},
614730
success: function(data) {
615731
if (data.items.length === 0) {
616732
window.location.href = makeSearchURL(formValue);
617733
}
618734
else {
735+
tags = data.items[0].snippet.tags;
619736
window.location.href = makeListenURL(videoID, formValueTime);
620737
}
621738
}
622739
}).fail(function(jqXHR, textStatus, errorThrown) {
623740
logError(jqXHR, textStatus, errorThrown, "Lookup error");
624741
});
742+
// fetching next videoIds for auto play
743+
fetchSuggestedVideoIds();
625744
}
626745
}
627746
else {
@@ -654,6 +773,8 @@ $(function() {
654773
// Handle demo link click
655774
$("#demo").click(function(event) {
656775
event.preventDefault();
776+
resetAutoPlayList();
777+
657778
gtag("send", "event", "demo", "clicked");
658779

659780
// Don't continue appending to the URL if it appears "good enough".
@@ -670,13 +791,22 @@ $(function() {
670791
// Handle focus link click
671792
$("#focus-btn").click(function(event) {
672793
event.preventDefault();
794+
resetAutoPlayList();
795+
673796
gtag("send", "event", "focus", "clicked");
674797
// Redirect to the favorite "focus" URL
675798
window.location.href = makeListenURL(focusId);
676799
});
677800

801+
// handle click on search icon
802+
$("#submit").click(function() {
803+
resetAutoPlayList();
804+
});
805+
678806
// Check if the current ID is the focus ID
679807
$(window).on("load", function() {
808+
loadAutoPlayDetails();
809+
680810
// Show Focus Button
681811
if (window.location.href.indexOf(focusId) === -1) {
682812
$("#focus-btn").show();

js/zap-common.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function getYouTubeVideoDescription(videoID, youTubeDataApiKey, onSuccess, onFai
2727
data: {
2828
key: youTubeDataApiKey,
2929
part: "snippet",
30-
fields: "items/snippet/description",
3130
id: videoID
3231
},
3332
success: onSuccess

0 commit comments

Comments
 (0)