94 lines
2.8 KiB
HTML
94 lines
2.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<title>Music Player</title>
|
|
<style>
|
|
body { font-family: sans-serif; padding: 1rem; }
|
|
button { margin: 0.5rem; }
|
|
#queueList { margin-top: 1rem; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Music Player</h1>
|
|
|
|
<div>
|
|
<h2>Player Controls</h2>
|
|
<button onclick="play()">Play/Pause</button>
|
|
<button onclick="stop()">Stop</button>
|
|
<button onclick="skip()">Skip</button>
|
|
</div>
|
|
|
|
<div>
|
|
<h2>Volume</h2>
|
|
<input type="range" min="0" max="1" step="0.01" id="volumeSlider" oninput="setVolume(this.value)">
|
|
<span id="volumeValue">0</span>
|
|
</div>
|
|
|
|
<div>
|
|
<h2>Queue</h2>
|
|
<input type="text" id="trackUrl" placeholder="Track URL">
|
|
<button onclick="addToQueue()">Add to Queue</button>
|
|
<div id="queueList"></div>
|
|
</div>
|
|
|
|
<div>
|
|
<h2>Player State</h2>
|
|
<pre id="playerState"></pre>
|
|
</div>
|
|
|
|
<script>
|
|
const api = (endpoint, options = {}) =>
|
|
fetch(endpoint, options).then(res => res.json()).catch(console.error);
|
|
|
|
const setVolume = async (val) => {
|
|
await api(`/player/volume?volume=${val}`, { method: "PUT" });
|
|
document.getElementById("volumeValue").textContent = val;
|
|
};
|
|
|
|
const addToQueue = async () => {
|
|
const url = document.getElementById("trackUrl").value;
|
|
await api(`/queue?url=${encodeURIComponent(url)}`, { method: "POST" });
|
|
updateQueue();
|
|
};
|
|
|
|
const play = async () => { await api("/player/play", { method: "POST" });};
|
|
const stop = async () => { await api("/player/stop", { method: "POST" });};
|
|
const skip = async () => { await api("/player/skip", { method: "POST" });};
|
|
|
|
// WebSocket connections
|
|
let playerSocket, queueSocket;
|
|
|
|
function connectWebSockets() {
|
|
const proto = location.protocol === "https:" ? "wss" : "ws";
|
|
const base = `${proto}://${location.host}`;
|
|
|
|
playerSocket = new WebSocket(`${base}/player`);
|
|
queueSocket = new WebSocket(`${base}/queue`);
|
|
|
|
playerSocket.onopen = () => playerSocket.send("ping");
|
|
queueSocket.onopen = () => queueSocket.send("ping");
|
|
|
|
playerSocket.onmessage = (event) => {
|
|
const state = JSON.parse(event.data);
|
|
document.getElementById("playerState").textContent = JSON.stringify(state, null, 2);
|
|
document.getElementById("volumeSlider").value = state.volume;
|
|
document.getElementById("volumeValue").textContent = state.volume;
|
|
};
|
|
|
|
queueSocket.onmessage = (event) => {
|
|
const queue = JSON.parse(event.data);
|
|
document.getElementById("queueList").textContent = JSON.stringify(queue.items, null, 2);
|
|
};
|
|
|
|
playerSocket.onerror = queueSocket.onerror = console.error;
|
|
playerSocket.onclose = () => setTimeout(connectWebSockets, 1000);
|
|
queueSocket.onclose = () => setTimeout(connectWebSockets, 1000);
|
|
}
|
|
|
|
connectWebSockets();
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|