-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathradioPlayer.js
More file actions
234 lines (212 loc) · 8.95 KB
/
radioPlayer.js
File metadata and controls
234 lines (212 loc) · 8.95 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
Copyright (C) 2021 Michael Slíva <michael@sliva.dev>
Licensed under the Academic Free License version 3.0
You should have received a copy of the Academic Free License version 3.0
along with this program. If not, see <https://spdx.org/licenses/AFL-3.0.html>.
*/
if (!jQuery) throw new Error("jQuery is not loaded!");
const checkboxRememberVolume = document.getElementById("rememberVolume");
const radio = document.getElementById("radio");
const streamURL = "https://listen.jetstreamradio.com:8000/autodj";
const radioAPIEndpointURL = './playing.php'; // using a local intermediary instead of using API endpoint directly because of CORS protection
var playerPositionTimer;
var playerResizeTimer;
var playerResizeTimer2;
var currentlyPlayingTimer;
var animationTimer;
var animationTimerTime = 0;
var w = 245 - $("#playerInfoHeading").width();
$("#playerInfoPlaying").css("min-width", w);
var d = new Date();
var expireDays = 3650;
d.setTime(d.getTime() + (expireDays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
function getTime() {
var r = "?time=" + new Date().getTime();
return r;
}
function getCookieValue(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function IsRememberVolume() {
var result = Number(getCookieValue("rememberVolume"));
if (result == 1) {
return true;
} else {
return false;
}
}
function resetVolume() {
if (!IsRememberVolume()){
radio.volume = 0.25;
document.getElementById("volume").value = "25";
checkboxRememberVolume.checked = false;
} else {
var savedVolume = Number(getCookieValue("savedVolume"));
radio.volume = savedVolume;
document.getElementById("volume").value = String(savedVolume * 100);
checkboxRememberVolume.checked = true;
}
}
resetVolume();
function getVolume() {
if (!IsRememberVolume()){
return 0.25;
} else {
return Number(getCookieValue("savedVolume"));
}
}
function radioPlayerPause() {
$("#radio").animate({volume: 0}, 2000);
setTimeout(function(){
radio.pause();
radio.setAttribute("src", "");
setTimeout(function() {
radio.load(); // This stops the stream from downloading
});
}, 2000);
clearInterval(currentlyPlayingTimer);
$(".fa-pause-circle").css("display", "none");
$(".fa-play-circle").css("display", "initial");
$("#equaliser-container").css("opacity", 0);
$("#playerInfo").css("transition", "opacity 1s ease");
$("#playerInfo").css("opacity", 0);
$("#playerAbout").css("animation", "helpMoveClickPause 0.5s ease 1.5s");
if ((animationTimerTime) >= 5 && (animationTimerTime <= 10)) {
$("#playerProviderLogo").css("animation", "playerLogoFadeStopClan 3s");
$("#playerRadioStationLogo").css("animation", "playerLogoFadeStopJet 3s 0s");
} else {
$("#playerProviderLogo").css("animation", "playerLogoFadeStop 3s");
$("#playerRadioStationLogo").css("animation", "");
}
playerResizeTimer = setTimeout(function(){
$("#playerMiddleLine").css("top", "-24px");
$("#playerBottomLine").css("top", "-24px");
$("#currentVolume").css("top", "-24px");
$("#MagicPlayer").css("height", "76px");
}, 1000);
playerResizeTimer2 = setTimeout(function(){
$("#MagicPlayer").css("width", "228px");
$("#playerLogo").css("left", "0px");
}, 1500);
playerPositionTimer = setTimeout(function(){
$("#playerAbout").css("left", "-22px");
}, 2000);
clearInterval(animationTimer);
$(".equaliser-column:nth-child(1) .colour-bar").css("animation", "color-bar-paused 2s 1s ease-out alternate infinite");
$(".equaliser-column:nth-child(2) .colour-bar").css("animation", "color-bar-paused 2s 0.5s ease-out alternate infinite");
$(".equaliser-column:nth-child(3) .colour-bar").css("animation", "color-bar-paused 2s 1.5s ease-out alternate infinite");
$(".equaliser-column:nth-child(4) .colour-bar").css("animation", "color-bar-paused 2s 0.375s ease-out alternate infinite");
$(".equaliser-column:nth-child(5) .colour-bar").css("animation", "color-bar-paused 2s 2s ease-out alternate infinite");
}
function radioPlayerPlay() {
clearTimeout(playerPositionTimer);
clearTimeout(playerResizeTimer);
clearTimeout(playerResizeTimer2);
animationTimer = setInterval(currentAnimation, 1000);
radio.setAttribute("src", streamURL + getTime());
radio.load();
radio.play();
$("#radio").animate({volume: 0}, 0);
$("#radio").animate({volume: getVolume()}, 2000);
$(".fa-play-circle").css("display", "none");
$(".fa-pause-circle").css("display", "initial");
$("#playerAbout").css("animation", "helpMoveClickPlay 0.5s ease");
$("#playerAbout").css("left", "0px");
$("#equaliser-container").css("opacity", 1);
$("#MagicPlayer").css("height", "100px");
$("#MagicPlayer").css("width", "245px");
$("#playerLogo").css("left", "17px");
$("#playerMiddleLine").css("top", "0px");
$("#playerBottomLine").css("top", "0px");
$("#currentVolume").css("top", "0px");
$("#playerInfo").css("transition", "opacity 3s ease-in");
$("#playerInfo").css("opacity", 1);
$("#playerProviderLogo").css("animation", "playerLogoFade 10s infinite");
$("#playerRadioStationLogo").css("animation", "playerLogoFade 10s 5s infinite");
currentlyPlaying();
currentlyPlayingTimer = setInterval(currentlyPlaying, 10000);
$(".equaliser-column:nth-child(1) .colour-bar").css("animation", "color-bar-playing 2s 1s ease-out alternate infinite");
$(".equaliser-column:nth-child(2) .colour-bar").css("animation", "color-bar-playing 2s 0.5s ease-out alternate infinite");
$(".equaliser-column:nth-child(3) .colour-bar").css("animation", "color-bar-playing 2s 1.5s ease-out alternate infinite");
$(".equaliser-column:nth-child(4) .colour-bar").css("animation", "color-bar-playing 2s 0.375s ease-out alternate infinite");
$(".equaliser-column:nth-child(5) .colour-bar").css("animation", "color-bar-playing 2s 2s ease-out alternate infinite");
}
function setVolume(val) {
clearTimeout(fadeoutTimer);
radio.volume = val / 100;
if (IsRememberVolume()) {
document.cookie = "savedVolume=" + radio.volume + ";" + expires + ";path=/;SameSite=Strict;Secure";
}
$("#currentVolume").css("transition", "top 0.5s ease 0s, opacity 0.5s ease 0s");
$("#currentVolume").css("opacity", 1);
document.getElementById("currentVolume").innerHTML = "Volume: " + val + "%";
var fadeoutTimer = setTimeout(function(){
$("#currentVolume").css("transition", "top 0.5s ease 0s, opacity 2.5s cubic-bezier(.17,.84,.44,1) 2s");
$("#currentVolume").css("opacity", 0);
}, 500);
}
function rememberCheck() {
if (checkboxRememberVolume.checked){
document.cookie = "rememberVolume=1;" + expires + ";path=/;SameSite=Strict;Secure";
document.cookie = "savedVolume=" + radio.volume + ";" + expires + ";path=/;SameSite=Strict;Secure";
} else {
document.cookie = "rememberVolume=0;" + expires + ";path=/;SameSite=Strict;Secure";
}
}
function currentlyPlaying() {
var songGetter = radioAPIEndpointURL + getTime();
$.ajax({
url: songGetter,
dataType: "text",
success: function(currentSong) {
currentSong = currentSong.replaceAll('&', '&');
// sanitize the song name for XSS injection in case it's not properly sanitized by the API
currentSong = currentSong.replaceAll('<', '<');
currentSong = currentSong.replaceAll('>', '>');
currentSong = currentSong.replaceAll('"', '"');
currentSong = currentSong.replaceAll('\'', ''');
currentSong = currentSong.replaceAll('/', '/');
currentSong = currentSong.replaceAll('\\', '\');
currentSong = currentSong.replaceAll('\n', '<br>');
currentSong = currentSong.replaceAll('\r', '<br>');
currentSong = currentSong.replaceAll('\t', ' ');
// sanitized
$( "#playerInfoPlaying" ).html(currentSong);
if (typeof songToHistory == 'function') songToHistory(currentSong); // standalone player does not contain this function, only available on full player page
}
});
}
function currentAnimation() {
animationTimerTime++;
if (animationTimerTime === 10) {
animationTimerTime = 0;
}
}
/* MediaSession support */
try {
navigator.mediaSession.setActionHandler('play', function() {
radioPlayerPlay();
});
navigator.mediaSession.setActionHandler('pause', function() {
radioPlayerPause();
});
navigator.mediaSession.setActionHandler('stop', function() {
radioPlayerPause();
});
} catch(error) {
log('Warning! Media session actions are not supported.' + error.message);
}
document.getElementById('playerAbout').title = "MagicD3VIL's Radio Player\nCopyright (C) 2021 Michael Slíva <michael@sliva.dev>\nLicensed under the Academic Free License version 3.0";