-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
62 lines (55 loc) · 1.54 KB
/
audio.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
var node = document.getElementById('main');
// the vorleser-server docker container changes these to point to
// window.location.href and hides the url field
var flags = {
serverUrl: "",
hideUrlField: false,
};
var app = Elm.Main.embed(node, flags);
var audio = new Audio();
setInterval(() => {
sendProgress()
}, 500);
app.ports.command.subscribe(function(command) {
if (command.command == "SetFile") {
audio.pause();
delete audio;
audio = new Audio(command.arg.file);
audio.currentTime = command.arg.position
audio.volume = command.arg.volume || 1
audio.addEventListener("canplay", () => {
app.ports.ready.send(audio.position || 0)
});
} else if (command.command === "Stop") {
audio.pause()
audio = new Audio()
} else if (command.command === "Play") {
audio.play();
} else if (command.command === "Pause") {
audio.pause();
} else if (command.command === "Unpause") {
audio.play();
} else if (command.command === "SetVolume") {
setVolume(command.arg.volume);
} else if (command.command === "Toggle") {
if (audio.paused) {
audio.play();
} else {
audio.pause()
}
} else if (command.command === "SkipTo") {
audio.currentTime = command.arg.position;
}
sendPlaying();
})
function setVolume(volume) {
audio.volume = volume
app.ports.volume.send(audio.volume)
window.localStorage.setItem("volume", volume);
}
function sendProgress() {
app.ports.progress.send(audio.currentTime || 0)
}
function sendPlaying() {
app.ports.playing.send(!audio.paused)
}