forked from talkol/spiderman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
radio.js
67 lines (61 loc) · 1.66 KB
/
radio.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
AFRAME.registerComponent("radio", {
schema: {
tracks: { type: "array", default: [] },
weights: { type: "array", default: [] }
},
init: function() {
try {
if (typeof SC !== "undefined") {
SC.client_id = "9fac4561902d4ea62413392ed74aa2b0";
SC.initialize({
client_id: SC.client_id
});
} else {
SC = null;
}
} catch (e) {
SC = null;
}
this.lastTrack = -1;
},
chooseTrack: function() {
let totalWeight = 0;
for (let i = 0; i < this.data.weights.length; i++) {
totalWeight += Number(this.data.weights[i]);
}
// try several times to randomize a new track
for (let attempts = 0; attempts < 5; attempts++) {
let chosen = Math.floor(Math.random() * totalWeight);
let weightSoFar = 0;
for (let i = 0; i < this.data.weights.length; i++) {
weightSoFar += Number(this.data.weights[i]);
if (weightSoFar > chosen) {
if (i != this.lastTrack) {
this.lastTrack = i;
return this.data.tracks[i];
} else break;
}
}
}
// default
return this.data.tracks[0];
},
play: function() {
let self = this;
if (SC == null) return;
let playing = false;
let button = document.getElementsByClassName("a-enter-vr-button")[0];
button.addEventListener("click", function() {
if (playing) return;
playing = true;
function playSound() {
SC.stream(`/tracks/${self.chooseTrack()}`).then(function(player) {
player.setVolume(0.5);
player.play();
player.on("finish", playSound);
});
}
playSound();
});
}
});