-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
46 lines (39 loc) · 1.65 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
<html>
<head>
<script type="application/javascript" src="https://unpkg.com/mqtt@3.0.0/dist/mqtt.min.js"></script>
</head>
<body style="margin: 0px;">
<audio controls id="audio" autoplay muted loop>
<source id="audioSource" src="" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<input type="range" id="volumeSlider" min="0" max="100" value="0" oninput="setVolume(); return false;">
<form action="" method="post" onsubmit="setTrack(); return false;">
<input type="text" id="track" style="width:400;">
</form>
<script>
document.getElementById("audio").volume = 0.5
document.getElementById("volumeSlider").value = 50
const urlParams = new URLSearchParams(window.location.search);
const base = urlParams.get("base")
const trackTopic = `${base}/track`
const volumeTopic = `${base}/volume`
const client = window.mqtt.connect("wss://test.mosquitto.org:8081")
client.subscribe(trackTopic)
client.on("message", (topic, payload) => {
console.log(topic + " " + payload.toString())
if (topic === trackTopic) {
document.getElementById("audioSource").src = payload.toString()
document.getElementById("audio").load()
document.getElementById("audio").play()
}
})
function setTrack() {
client.publish(trackTopic, document.getElementById("track").value)
}
function setVolume() {
document.getElementById("audio").volume = document.getElementById("volumeSlider").value / 100
}
</script>
</body>
</html>