-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
204 lines (182 loc) · 6.34 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Media Key Sync</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
.hidden {
display: none;
}
.mediaKeyButton {
background-color: Transparent;
background-repeat: no-repeat;
border: none;
width: 50px;
height: 50px;
color: #eeeeee;
cursor: pointer;
}
.mediaKeyButton:hover {
color: #aaaaaa;
}
</style>
<script>
let audio = document.createElement("audio");
audio.src = "15-seconds-of-silence.mp3";
audio.loop = true;
audio.volume = 0.01; // muted does not work...
var syncWebsocket = null;
navigator.mediaSession.setActionHandler("play", play);
navigator.mediaSession.setActionHandler("pause", pause);
navigator.mediaSession.setActionHandler("previoustrack", skipPrevious);
navigator.mediaSession.setActionHandler("nexttrack", skipNext);
function displayInfo(info) {
document.getElementById("title").innerText = info.title;
document.getElementById("artist").innerText = info.artist;
document.getElementById("application").innerText = info.app;
document.getElementById("connection-panel").classList.add("hidden");
document.getElementById("playing-panel").classList.remove("hidden");
let cover_src = "data:image/png;base64," + info.thumbnailBase64Png;
document.getElementById("cover").src = cover_src;
navigator.mediaSession.metadata = new MediaMetadata({
title: info.title,
artist: info.artist,
artwork: [{ src: cover_src, type: "image/png" }],
});
}
function connectToServer(ip, port) {
syncWebsocket = new WebSocket("ws://" + ip + ":" + port);
audio.play();
audio.pause();
syncWebsocket.onopen = () => syncWebsocket.send("info");
syncWebsocket.onmessage = (event) => {
var msg = JSON.parse(event.data);
switch (msg.type) {
case "error":
alert(msg.message);
break;
case "mediainfo":
console.log(msg.info);
displayInfo(msg.info);
break;
}
};
}
async function play() {
console.log('> User clicked "Play" icon.');
await audio.play();
syncWebsocket.send("play");
}
async function pause() {
console.log('> User clicked "Pause" icon.');
audio.pause();
syncWebsocket.send("pause");
}
async function skipPrevious() {
console.log('> User clicked "Previous Track" icon.');
syncWebsocket.send("prev");
syncWebsocket.send("play");
await audio.play();
}
async function skipNext() {
console.log('> User clicked "Next Track" icon.');
syncWebsocket.send("next");
syncWebsocket.send("play");
await audio.play();
}
function connectClick() {
let ip = document.getElementById("ip").value;
let port = document.getElementById("port").value;
connectToServer(ip, port);
window.localStorage.setItem("mediaSyncIP", ip);
window.localStorage.setItem("mediaSyncPort", port);
}
window.addEventListener("load", function () {
let storedIP = window.localStorage.getItem("mediaSyncIP");
let storedPort = window.localStorage.getItem("mediaSyncPort");
if (storedIP) {
document.getElementById("ip").value = storedIP;
}
if (storedPort) {
document.getElementById("port").value = storedPort;
}
});
</script>
</head>
<body style="background-color: #111111; color: #dddddd">
<div id="connection-panel">
<h1>Connect to server</h1>
<label for="ip">Hostname / IP</label>
<input type="text" name="ip" id="ip" value="127.0.0.1" />
<label for="port">Port</label>
<input
type="number"
name="port"
id="port"
min="1"
max="65535"
value="5000"
size="6"
/>
<input type="button" onclick="connectClick()" value="Connect" />
</div>
<div id="playing-panel" class="hidden">
<h1>Currently Playing:</h1>
<h2 id="title">Title</h2>
<h3 id="artist">Artist</h3>
<h3 id="application">Application</h3>
<img id="cover" src="data:image/png;base64," alt="Cover" />
<button class="mediaKeyButton" onclick="skipPrevious()">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"
fill="currentColor"
>
<path
d="M48 256c0 114.69 93.31 208 208 208s208-93.31 208-208S370.69 48 256 48 48 141.31 48 256zm128-64a16 16 0 0132 0v53l111.68-67.46a10.78 10.78 0 0116.32 9.33v138.26a10.78 10.78 0 01-16.32 9.31L208 267v53a16 16 0 01-32 0z"
/>
</svg>
</button>
<button class="mediaKeyButton" onclick="play()">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z"
clip-rule="evenodd"
/>
</svg>
</button>
<button class="mediaKeyButton" onclick="pause()">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zM7 8a1 1 0 012 0v4a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v4a1 1 0 102 0V8a1 1 0 00-1-1z"
clip-rule="evenodd"
/>
</svg>
</button>
<button class="mediaKeyButton" onclick="skipNext()">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"
fill="currentColor"
>
<path
d="M256 48C141.31 48 48 141.31 48 256s93.31 208 208 208 208-93.31 208-208S370.69 48 256 48zm80 272a16 16 0 01-32 0v-53l-111.68 67.44a10.78 10.78 0 01-16.32-9.31V186.87a10.78 10.78 0 0116.32-9.31L304 245v-53a16 16 0 0132 0z"
/>
</svg>
</button>
</div>
</body>
</html>