-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkristen_riks.js
101 lines (86 loc) · 3.09 KB
/
kristen_riks.js
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
const audioPlayer = document.getElementById('audio-player');
const playButton = document.getElementById('play-button');
const volumeSlider = document.querySelector('#volume-slider input');
const socket = new WebSocket("wss://radio.p7.no/api/live/nowplaying/websocket");
playButton.addEventListener('click', () => {
if (audioPlayer.paused) {
audioPlayer.play();
playButton.innerHTML = '<i class="fas fa-stop"></i>';
} else {
audioPlayer.pause();
playButton.innerHTML = '<i class="fas fa-play"></i>';
}
});
volumeSlider.addEventListener('input', () => {
audioPlayer.volume = volumeSlider.value / 100;
});
const volumeButton = document.getElementById('volume-button');
volumeButton.addEventListener('click', () => {
if (audioPlayer.muted) {
audioPlayer.muted = false;
volumeButton.innerHTML = '<i class="fas fa-volume-up"></i>';
} else {
audioPlayer.muted = true;
volumeButton.innerHTML = '<i class="fas fa-volume-mute"></i>';
}
});
volumeSlider.addEventListener('input', () => {
if (volumeSlider.value === '0') {
volumeButton.innerHTML = '<i class="fas fa-volume-mute"></i>';
} else if (volumeSlider.value > 0 && volumeSlider.value < 50) {
volumeButton.innerHTML = '<i class="fas fa-volume-down"></i>';
} else {
volumeButton.innerHTML = '<i class="fas fa-volume-up"></i>';
}
});
volumeSlider.addEventListener('input', () => {
if (audioPlayer.muted) {
audioPlayer.muted = false;
volumeButton.innerHTML = '<i class="fas fa-volume-up"></i>';
}
});
socket.onopen = function(e) {
socket.send(JSON.stringify({
"subs": {
"station:p7_kristen_riksradio": {"recover": true}
}
}));
}
let nowplaying = {};
let currentTime = 0;
function handleSseData(ssePayload, useTime = true) {
const jsonData = ssePayload.data;
if (useTime && 'current_time' in jsonData) {
currentTime = jsonData.current_time;
}
nowPlaying = jsonData.np.now_playing;
nowplayingArtist = jsonData.np.now_playing.song.artist;
nowplayingTitle = jsonData.np.now_playing.song.title;
nowplayingArt = jsonData.np.now_playing.song.art;
document.getElementById('track-title-text').textContent = nowplayingTitle;
document.getElementById('track-artist-text').textContent = nowplayingArtist;
document.getElementById('cover-art-image').src = nowplayingArt;
}
socket.onmessage = function(e) {
const jsonData = JSON.parse(e.data);
if ('connect' in jsonData) {
const connectData = jsonData.connect;
if ('data' in connectData) {
connectData.data.forEach(
(initialRow) => handleSseData(initialRow)
);
} else {
if ('time' in connectData) {
currentTime = Math.floor(connectData.time / 1000);
}
for (const subName in connectData.subs) {
const sub = connectData.subs[subName];
if ('publications' in sub && sub.publications.length > 0) {
sub.publications.forEach((initialRow) => handleSseData(initialRow, false));
}
}
}
} else if ('pub' in jsonData) {
handleSseData(jsonData.pub);
}
};