-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicplayer.js
77 lines (64 loc) · 2.06 KB
/
musicplayer.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
//@ts-check
var musicTracks;
var currentTrack;
/**
* Plays the background music and picks new tracks. Updated from the sketch.js
*/
class MusicPlayer{
static initialize(){
musicTracks = [];
musicTracks.push(music_AbovePlanets);
musicTracks.push(music_Cages);
musicTracks.push(music_LostConstructs);
currentTrack = MusicPlayer.pickRandomTrack();
/*
currentTrack = MusicPlayer.pickRandomTrack();
currentTrack.setVolume(0.4);
currentTrack.play();
*/
this.isFirstTimePickingMusic = true;
}
static update(){
if(!currentTrack.isPlaying()){
currentTrack=null;
}
/*
if(currentTrack==null){
if(this.isFirstTimePickingMusic){
currentTrack = music_Cages; //this is always the first track played
} else {
currentTrack = MusicPlayer.pickRandomTrack();
console.log("picking new track");
}
currentTrack.setVolume(0.4);
var response = currentTrack.play();
if (response!== undefined) {
response.then(_ => {
//the sound should start playing
this.isFirstTimePickingMusic = false;
}).catch(error => {
//simply handle the rror
});
}
}
*/
if(currentTrack==null){
currentTrack = MusicPlayer.pickRandomTrack();
console.log("picking new track");
currentTrack.setVolume(0.4);
var response = currentTrack.play();
if (response!== undefined) {
response.then(_ => {
//the sound should start playing
this.isFirstTimePickingMusic = false;
}).catch(error => {
//simply handle the rror
});
}
}
}
static pickRandomTrack(){
let i = Math.floor(random(0, musicTracks.length));
return musicTracks[i];
}
}