-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
92 lines (88 loc) · 2.47 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
// Image refresh
var last_refresh_image = 0;
var mediasession_enabled = false;
function refresh_image() {
$("img").each(function () {
$(this).attr("src", $(this).attr("src") + "?" + Math.random());
last_refresh_image = Date.now();
});
}
// Times and progress bar refresh
function refresh_times() {
$.get("/times", {}, function (data) {
$("#current").html(data.current);
$("#total").html(data.total);
$("#position").css("width", data.perc + "%");
if ("mediaSession" in navigator) {
$("#audio")[0].currentTime = data.current_s;
navigator.mediaSession.setPositionState({
duration: data.total_s,
position: data.current_s,
});
}
});
}
$(document).ready(function () {
refresh_times();
// Refresh timers
setInterval(function () {
refresh_times();
}, 1000);
setInterval(function () {
if (Date.now() - last_refresh_image > 3000) {
refresh_image();
}
}, 1000);
// Seeking by clicking on the progress bar
$("#bar").click(function (e) {
enable_mediasession();
var perc = (100.0 * (e.pageX - $(this).parent().offset().left)) / $("#bar").width();
$.get("/action/seek", { position: perc }, function () {
refresh_image();
});
});
// Clicking on the action buttons
$(".action").click(function () {
enable_mediasession();
action(this.id);
});
});
function action(id) {
$.get("/action/" + id, {}, function () {
refresh_times();
refresh_image();
if ("mediaSession" in navigator) {
if (id == "play") {
navigator.mediaSession.playbackState = "playing";
audio.play();
} else if (id == "pause") {
navigator.mediaSession.playbackState = "paused";
audio.pause();
}
}
});
}
function enable_mediasession() {
if (mediasession_enabled) {
return;
}
var audio = $("#audio")[0];
audio.play();
if ("mediaSession" in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: "mpv",
artwork: [{ src: "/screenshot", sizes: "300x300", type: "image/jpg" }],
});
navigator.mediaSession.setActionHandler("play", () => {
audio.play();
action("play");
});
navigator.mediaSession.setActionHandler("pause", () => {
audio.pause();
action("pause");
});
navigator.mediaSession.setActionHandler("seekbackward", (details) => action("rewind"));
navigator.mediaSession.setActionHandler("previoustrack", () => action("rewind"));
}
mediasession_enabled = true;
}