-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscript.js
536 lines (465 loc) · 17.4 KB
/
script.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
const RADIO_NAME = 'Game! Radio 1';
// SELECT ARTWORK PROVIDER, ITUNES, DEEZER & SPOTIFY eg : spotify
var API_SERVICE = 'deezer';
// Change Stream URL Here, Supports, ICECAST, ZENO, SHOUTCAST, RADIOJAR ETC.... DOES NOT SUPPORT HLS
const URL_STREAMING = 'https://stream-51.zeno.fm/cfhkm5fs1uhvv?zs=HOu6hxV1SG-7iGi9WGVTqQ';
//NOW PLAYING API.
const API_URL = 'https://prod-api.radioapi.me/streamtitle/8ce17c30-2fc5-4f1d-a6d8-4ac3ca9b35b6';
// Visit https://api.vagalume.com.br/docs/ to get your API key
const API_KEY = "18fe07917957c289983464588aabddfb";
window.onload = function () {
var page = new Page;
page.changeTitlePage();
page.setVolume();
var player = new Player();
player.play();
getStreamingData();
// Interval to get streaming data in miliseconds
setInterval(function () {
getStreamingData();
}, 10000);
var coverArt = document.getElementsByClassName('cover-album')[0];
coverArt.style.height = coverArt.offsetWidth + 'px';
}
// DOM control
function Page() {
this.changeTitlePage = function (title = RADIO_NAME) {
document.title = title;
};
this.refreshCurrentSong = function (song, artist) {
var currentSong = document.getElementById('currentSong');
var currentArtist = document.getElementById('currentArtist');
if (song !== currentSong.innerHTML) {
// Animate transition
currentSong.className = 'animated flipInY text-uppercase';
currentSong.innerHTML = song;
currentArtist.className = 'animated flipInY text-capitalize';
currentArtist.innerHTML = artist;
// Refresh modal title
document.getElementById('lyricsSong').innerHTML = song + ' - ' + artist;
// Remove animation classes
setTimeout(function () {
currentSong.className = 'text-uppercase';
currentArtist.className = 'text-capitalize';
}, 2000);
}
}
this.refreshHistoric = function (info, n) {
var $historicDiv = document.querySelectorAll('#historicSong article');
var $songName = document.querySelectorAll('#historicSong article .music-info .song');
var $artistName = document.querySelectorAll('#historicSong article .music-info .artist');
// Default cover art
var urlCoverArt = 'img/cover.png';
// Get cover art for song history
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
var data = JSON.parse(this.responseText);
var artwork = data.results.artwork;
var artworkXL = artwork.large;
document.querySelectorAll('#historicSong article .cover-historic')[n].style.backgroundImage = 'url(' + artworkXL + ')';
}
// Formating characters to UTF-8
var music = info.song.replace(/'/g, '\'');
var songHist = music.replace(/&/g, '&');
var artist = info.artist.replace(/'/g, '\'');
var artistHist = artist.replace(/&/g, '&');
$songName[n].innerHTML = songHist;
$artistName[n].innerHTML = artistHist;
// Add class for animation
$historicDiv[n].classList.add('animated');
$historicDiv[n].classList.add('slideInRight');
}
xhttp.open('GET', 'https://prod-api.radioapi.me/1ceb9727-3e36-4e64-99e7-f776b50c7f4f/musicsearch?query=' + info.artist + ' ' + info.song);
xhttp.send();
setTimeout(function () {
for (var j = 0; j < 2; j++) {
$historicDiv[j].classList.remove('animated');
$historicDiv[j].classList.remove('slideInRight');
}
}, 2000);
}
this.refreshCover = function (song = '', artist) {
// Default cover art
var urlCoverArt = 'img/cover.png';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
var coverArt = document.getElementById('currentCoverArt');
var coverBackground = document.getElementById('bgCover');
// Get cover art URL on iTunes API
if (this.readyState === 4 && this.status === 200) {
var data = JSON.parse(this.responseText);
var artworkUrl100 = data.results;
var urlCoverArt = artworkUrl100.artwork.medium;
coverArt.style.backgroundImage = 'url(' + urlCoverArt + ')';
coverArt.className = 'animated bounceInLeft';
coverBackground.style.backgroundImage = 'url(' + urlCoverArt + ')';
setTimeout(function () {
coverArt.className = '';
}, 2000);
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: song,
artist: artist,
artwork: [{
src: urlCoverArt,
sizes: '96x96',
type: 'image/png'
},
{
src: urlCoverArt,
sizes: '128x128',
type: 'image/png'
},
{
src: urlCoverArt,
sizes: '192x192',
type: 'image/png'
},
{
src: urlCoverArt,
sizes: '256x256',
type: 'image/png'
},
{
src: urlCoverArt,
sizes: '384x384',
type: 'image/png'
},
{
src: urlCoverArt,
sizes: '512x512',
type: 'image/png'
}
]
});
}
}
}
xhttp.open('GET', 'https://prod-api.radioapi.me/1ceb9727-3e36-4e64-99e7-f776b50c7f4f/musicsearch?query=' + artist + ' ' + song);
xhttp.send();
}
this.changeVolumeIndicator = function (volume) {
document.getElementById('volIndicator').innerHTML = volume;
if (typeof (Storage) !== 'undefined') {
localStorage.setItem('volume', volume);
}
}
this.setVolume = function () {
if (typeof (Storage) !== 'undefined') {
var volumeLocalStorage = (!localStorage.getItem('volume')) ? 80 : localStorage.getItem('volume');
document.getElementById('volume').value = volumeLocalStorage;
document.getElementById('volIndicator').innerHTML = volumeLocalStorage;
}
}
this.refreshLyric = function (currentSong, currentArtist) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
var data = JSON.parse(this.responseText);
var openLyric = document.getElementsByClassName('lyrics')[0];
if (data.type === 'exact' || data.type === 'aprox') {
var lyric = data.mus[0].text;
document.getElementById('lyric').innerHTML = lyric.replace(/\n/g, '<br />');
openLyric.style.opacity = "1";
openLyric.setAttribute('data-toggle', 'modal');
} else {
openLyric.style.opacity = "0.3";
openLyric.removeAttribute('data-toggle');
var modalLyric = document.getElementById('modalLyrics');
modalLyric.style.display = "none";
modalLyric.setAttribute('aria-hidden', 'true');
(document.getElementsByClassName('modal-backdrop')[0]) ? document.getElementsByClassName('modal-backdrop')[0].remove(): '';
}
} else {
document.getElementsByClassName('lyrics')[0].style.opacity = "0.3";
document.getElementsByClassName('lyrics')[0].removeAttribute('data-toggle');
}
}
xhttp.open('GET', 'https://api.vagalume.com.br/search.php?apikey=' + API_KEY + '&art=' + currentArtist + '&mus=' + currentSong.toLowerCase(), true);
xhttp.send()
}
}
var audio = new Audio(URL_STREAMING);
// Player control
function Player() {
this.play = function () {
audio.play();
var defaultVolume = document.getElementById('volume').value;
if (typeof (Storage) !== 'undefined') {
if (localStorage.getItem('volume') !== null) {
audio.volume = intToDecimal(localStorage.getItem('volume'));
} else {
audio.volume = intToDecimal(defaultVolume);
}
} else {
audio.volume = intToDecimal(defaultVolume);
}
document.getElementById('volIndicator').innerHTML = defaultVolume;
};
this.pause = function () {
audio.pause();
};
}
// On play, change the button to pause
audio.onplay = function () {
var botao = document.getElementById('playerButton');
var bplay = document.getElementById('buttonPlay');
if (botao.className === 'fa fa-play') {
botao.className = 'fa fa-pause';
bplay.firstChild.data = 'PAUSE';
}
}
// On pause, change the button to play
audio.onpause = function () {
var botao = document.getElementById('playerButton');
var bplay = document.getElementById('buttonPlay');
if (botao.className === 'fa fa-pause') {
botao.className = 'fa fa-play';
bplay.firstChild.data = 'PLAY';
}
}
// Unmute when volume changed
audio.onvolumechange = function () {
if (audio.volume > 0) {
audio.muted = false;
}
}
audio.onerror = function () {
var confirmacao = confirm('Stream Down / Network Error. \nClick OK to try again.');
if (confirmacao) {
window.location.reload();
}
}
document.getElementById('volume').oninput = function () {
audio.volume = intToDecimal(this.value);
var page = new Page();
page.changeVolumeIndicator(this.value);
}
function togglePlay() {
if (!audio.paused) {
audio.pause();
} else {
audio.load();
audio.play();
}
}
function volumeUp() {
var vol = audio.volume;
if(audio) {
if(audio.volume >= 0 && audio.volume < 1) {
audio.volume = (vol + .01).toFixed(2);
}
}
}
function volumeDown() {
var vol = audio.volume;
if(audio) {
if(audio.volume >= 0.01 && audio.volume <= 1) {
audio.volume = (vol - .01).toFixed(2);
}
}
}
function mute() {
if (!audio.muted) {
document.getElementById('volIndicator').innerHTML = 0;
document.getElementById('volume').value = 0;
audio.volume = 0;
audio.muted = true;
} else {
var localVolume = localStorage.getItem('volume');
document.getElementById('volIndicator').innerHTML = localVolume;
document.getElementById('volume').value = localVolume;
audio.volume = intToDecimal(localVolume);
audio.muted = false;
}
}
function getStreamingData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
if (this.response.length === 0) {
console.log('%cdebug', 'font-size: 22px');
}
var data = JSON.parse(this.responseText);
console.log('Received data:', data); // Add this line for debugging
var page = new Page();
// Formating characters to UTF-8
let song = data.song ? data.song.replace(/'/g, '\'') : '';
let artist = data.artist ? data.artist.replace(/'/g, '\'') : '';
// Change the title
document.title = song + ' - ' + artist + ' | ' + RADIO_NAME;
if (document.getElementById('currentSong').innerHTML !== song) {
page.refreshCover(song, artist);
page.refreshCurrentSong(song, artist);
page.refreshLyric(song, artist);
for (var i = 0; i < 2; i++) {
page.refreshHistoric(data.history[i], i);
}
}
}
};
var d = new Date();
// Requisition with timestamp to prevent cache on mobile devices
xhttp.open('GET', API_URL);
xhttp.send();
}
// Player control by keys
document.addEventListener('keydown', function (k) {
var k = k || window.event;
var key = k.keyCode || k.which;
var slideVolume = document.getElementById('volume');
var page = new Page();
switch (key) {
// Arrow up
case 38:
volumeUp();
slideVolume.value = decimalToInt(audio.volume);
page.changeVolumeIndicator(decimalToInt(audio.volume));
break;
// Arrow down
case 40:
volumeDown();
slideVolume.value = decimalToInt(audio.volume);
page.changeVolumeIndicator(decimalToInt(audio.volume));
break;
// Spacebar
case 32:
togglePlay();
break;
// P
case 80:
togglePlay();
break;
// M
case 77:
mute();
break;
// 0
case 48:
audio.volume = 0;
slideVolume.value = 0;
page.changeVolumeIndicator(0);
break;
// 0 numeric keyboard
case 96:
audio.volume = 0;
slideVolume.value = 0;
page.changeVolumeIndicator(0);
break;
// 1
case 49:
audio.volume = .1;
slideVolume.value = 10;
page.changeVolumeIndicator(10);
break;
// 1 numeric key
case 97:
audio.volume = .1;
slideVolume.value = 10;
page.changeVolumeIndicator(10);
break;
// 2
case 50:
audio.volume = .2;
slideVolume.value = 20;
page.changeVolumeIndicator(20);
break;
// 2 numeric key
case 98:
audio.volume = .2;
slideVolume.value = 20;
page.changeVolumeIndicator(20);
break;
// 3
case 51:
audio.volume = .3;
slideVolume.value = 30;
page.changeVolumeIndicator(30);
break;
// 3 numeric key
case 99:
audio.volume = .3;
slideVolume.value = 30;
page.changeVolumeIndicator(30);
break;
// 4
case 52:
audio.volume = .4;
slideVolume.value = 40;
page.changeVolumeIndicator(40);
break;
// 4 numeric key
case 100:
audio.volume = .4;
slideVolume.value = 40;
page.changeVolumeIndicator(40);
break;
// 5
case 53:
audio.volume = .5;
slideVolume.value = 50;
page.changeVolumeIndicator(50);
break;
// 5 numeric key
case 101:
audio.volume = .5;
slideVolume.value = 50;
page.changeVolumeIndicator(50);
break;
// 6
case 54:
audio.volume = .6;
slideVolume.value = 60;
page.changeVolumeIndicator(60);
break;
// 6 numeric key
case 102:
audio.volume = .6;
slideVolume.value = 60;
page.changeVolumeIndicator(60);
break;
// 7
case 55:
audio.volume = .7;
slideVolume.value = 70;
page.changeVolumeIndicator(70);
break;
// 7 numeric key
case 103:
audio.volume = .7;
slideVolume.value = 70;
page.changeVolumeIndicator(70);
break;
// 8
case 56:
audio.volume = .8;
slideVolume.value = 80;
page.changeVolumeIndicator(80);
break;
// 8 numeric key
case 104:
audio.volume = .8;
slideVolume.value = 80;
page.changeVolumeIndicator(80);
break;
// 9
case 57:
audio.volume = .9;
slideVolume.value = 90;
page.changeVolumeIndicator(90);
break;
// 9 numeric key
case 105:
audio.volume = .9;
slideVolume.value = 90;
page.changeVolumeIndicator(90);
break;
}
});
function intToDecimal(vol) {
return vol / 100;
}
function decimalToInt(vol) {
return vol * 100;
}