-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.js
82 lines (71 loc) · 2.42 KB
/
music.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
class MusicPlayer {
constructor() {
if (!MusicPlayer.instance) {
this.audio = new Audio('audio/Underclocked - Eric Skiff.opus');
this.darkAudio = new Audio('audio/Ghost From The Future - C152.opus');
this.audio.loop = true;
this.darkAudio.loop = true;
this.audio.volume = 0.5;
this.darkAudio.volume = 0.5;
this.isPlayingDark = false;
// Store the current time before page unload
window.addEventListener('beforeunload', () => {
sessionStorage.setItem('musicTime', this.audio.currentTime);
});
// Preload both audio files
this.audio.load();
this.darkAudio.load();
MusicPlayer.instance = this;
}
return MusicPlayer.instance;
}
async play() {
try {
// Try to resume from the last stored position
const lastTime = sessionStorage.getItem('musicTime');
if (lastTime) {
this.audio.currentTime = parseFloat(lastTime);
}
await this.audio.play();
// Periodically store the current time
setInterval(() => {
if (!this.isPlayingDark) {
sessionStorage.setItem('musicTime', this.audio.currentTime);
}
}, 1000);
} catch (error) {
console.log("Playback failed:", error);
}
}
async playDarkMusic() {
try {
this.audio.pause();
this.darkAudio.currentTime = 0;
await this.darkAudio.play();
this.isPlayingDark = true;
} catch (error) {
console.log("Dark music playback failed:", error);
}
}
async stopDarkMusic() {
try {
this.darkAudio.pause();
this.darkAudio.currentTime = 0;
this.isPlayingDark = false;
await this.play();
} catch (error) {
console.log("Failed to restore original music:", error);
}
}
getCurrentTime() {
return this.isPlayingDark ? this.darkAudio.currentTime : this.audio.currentTime;
}
setCurrentTime(time) {
if (this.isPlayingDark) {
this.darkAudio.currentTime = time;
} else {
this.audio.currentTime = time;
}
}
}
const musicPlayer = new MusicPlayer();