add simple playnext
Signed-off-by: Christian Richter <crichter@owncloud.com>
This commit is contained in:
@@ -39,7 +39,7 @@
|
|||||||
<div class="title">
|
<div class="title">
|
||||||
<h1>Weirdradio</h1>
|
<h1>Weirdradio</h1>
|
||||||
</div>
|
</div>
|
||||||
<div id="playlist" class="playlist">{playlist}</div>
|
<div id="playlist" class="playlist"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="rightcolumn">
|
<div class="rightcolumn">
|
||||||
<div class="playerframe">
|
<div class="playerframe">
|
||||||
@@ -53,6 +53,8 @@
|
|||||||
var socketConnector = null;
|
var socketConnector = null;
|
||||||
var socketSemaphore = false;
|
var socketSemaphore = false;
|
||||||
var tag = document.createElement("script");
|
var tag = document.createElement("script");
|
||||||
|
var currentVideoID = null;
|
||||||
|
var player = null;
|
||||||
|
|
||||||
tag.src = "https://www.youtube.com/iframe_api";
|
tag.src = "https://www.youtube.com/iframe_api";
|
||||||
var firstScriptTag = document.getElementsByTagName("script")[0];
|
var firstScriptTag = document.getElementsByTagName("script")[0];
|
||||||
@@ -62,10 +64,11 @@
|
|||||||
// after the API code downloads.
|
// after the API code downloads.
|
||||||
var player;
|
var player;
|
||||||
function onYouTubeIframeAPIReady() {
|
function onYouTubeIframeAPIReady() {
|
||||||
|
currentVideoID = "Wch3gJG2GJ4";
|
||||||
player = new YT.Player("player", {
|
player = new YT.Player("player", {
|
||||||
height: "390",
|
height: "390",
|
||||||
width: "640",
|
width: "640",
|
||||||
videoId: "Wch3gJG2GJ4",
|
videoId: currentVideoID,
|
||||||
playerVars: {
|
playerVars: {
|
||||||
playsinline: 1,
|
playsinline: 1,
|
||||||
},
|
},
|
||||||
@@ -84,6 +87,8 @@
|
|||||||
function onPlayerStateChange(event) {
|
function onPlayerStateChange(event) {
|
||||||
if (event.data == 0) {
|
if (event.data == 0) {
|
||||||
console.log("playing stopped");
|
console.log("playing stopped");
|
||||||
|
removeFromPlaylist(currentVideoID);
|
||||||
|
playNext();
|
||||||
}
|
}
|
||||||
//if (event.data == YT.PlayerState.PLAYING && !done) {
|
//if (event.data == YT.PlayerState.PLAYING && !done) {
|
||||||
// setTimeout(stopVideo, 6000);
|
// setTimeout(stopVideo, 6000);
|
||||||
@@ -96,7 +101,9 @@
|
|||||||
|
|
||||||
function connectSocket() {
|
function connectSocket() {
|
||||||
if (socketSemaphore) {
|
if (socketSemaphore) {
|
||||||
console.log("Another connection attempt already in progress, canceling!");
|
console.log(
|
||||||
|
"Another connection attempt already in progress, canceling!"
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Connecting to socket");
|
console.log("Connecting to socket");
|
||||||
@@ -130,10 +137,7 @@
|
|||||||
socket.onmessage = function (event) {
|
socket.onmessage = function (event) {
|
||||||
data = JSON.parse(event.data);
|
data = JSON.parse(event.data);
|
||||||
if (data["videoId"] != undefined && data["videoId"] != null) {
|
if (data["videoId"] != undefined && data["videoId"] != null) {
|
||||||
playlist.push(data);
|
addToPlaylist(data);
|
||||||
updatePlaylist();
|
|
||||||
player.loadVideoById(data["videoId"]);
|
|
||||||
player.playVideo();
|
|
||||||
console.log(`[message] Data received from server: ${event.data}`);
|
console.log(`[message] Data received from server: ${event.data}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
@@ -144,14 +148,47 @@
|
|||||||
socketSemaphore = false;
|
socketSemaphore = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePlaylist() {
|
function removeFromPlaylist(videoId) {
|
||||||
pl = document.getElementById("playlist");
|
tmp = [];
|
||||||
output = "";
|
playlist.forEach((item) => {
|
||||||
playlist.forEach((e) => {
|
if (item.videoId != videoId) {
|
||||||
output =
|
tmp.push(item);
|
||||||
output + `<div><div>${e.title}</div><div>${e.link}</div></div>`;
|
} else {
|
||||||
|
console.log(`removing ${videoId}`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
pl.innerHTML = output;
|
playlist = tmp;
|
||||||
|
// remove from DOM
|
||||||
|
rm = document.getElementById(videoId);
|
||||||
|
if (rm != null) {
|
||||||
|
rm.remove();
|
||||||
|
}
|
||||||
|
currentVideoID = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function playNext() {
|
||||||
|
console.log(playlist)
|
||||||
|
console.log(playlist[0].videoId)
|
||||||
|
if (playlist[0] != null && playlist[0] != "") {
|
||||||
|
player.loadVideoById(playlist[0].videoId);
|
||||||
|
player.playVideo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addToPlaylist(data) {
|
||||||
|
if (document.getElementById(data.videoId) == null) {
|
||||||
|
playlist.push(data);
|
||||||
|
pl = document.getElementById(
|
||||||
|
"playlist"
|
||||||
|
).innerHTML += `<div id="${data.videoId}"><div>${data.title}</div><div><a target="_blank" href="${data.link}">${data.link}</a></div></div>`;
|
||||||
|
if (player.getPlayerState() == YT.PlayerState.ENDED) {
|
||||||
|
player.loadVideoById(data["videoId"]);
|
||||||
|
player.playVideo();
|
||||||
|
currentVideoID = data["videoId"];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log(`item ${data.videoId} already exists in playlist skipping`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//function myDebugger() {
|
//function myDebugger() {
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ console.log("Websocketserver started");
|
|||||||
let sockets = [];
|
let sockets = [];
|
||||||
webSocketServer.on("connection", function (socket) {
|
webSocketServer.on("connection", function (socket) {
|
||||||
sockets.push(socket);
|
sockets.push(socket);
|
||||||
|
console.log(`Connection from ${socket.remoteAddress}`);
|
||||||
|
|
||||||
// When you receive a message, send that message to every socket.
|
// When you receive a message, send that message to every socket.
|
||||||
//socket.on('message', function(msg) {
|
//socket.on('message', function(msg) {
|
||||||
@@ -62,6 +63,7 @@ webSocketServer.on("connection", function (socket) {
|
|||||||
|
|
||||||
// When a socket closes, or disconnects, remove it from the array.
|
// When a socket closes, or disconnects, remove it from the array.
|
||||||
socket.on("close", function () {
|
socket.on("close", function () {
|
||||||
|
console.log(`Disconnect from ${socket.remoteAddress}`);
|
||||||
sockets = sockets.filter((s) => s !== socket);
|
sockets = sockets.filter((s) => s !== socket);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user