-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathaudioEvents.ts
305 lines (223 loc) · 7.62 KB
/
audioEvents.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { audio, listAnchor, playButton, progress, queuelist, title } from "../lib/dom";
import player from "../lib/player";
import { convertSStoHHMMSS, getDownloadLink, goTo, notify, removeSaved, save } from "../lib/utils";
import { getSaved, params, store } from "../lib/store";
import { appendToQueuelist, firstItemInQueue } from "./queue";
import { addToCollection, getCollection } from "../lib/libraryUtils";
import { getData } from "../modules/getStreamData";
const playSpeed = <HTMLSelectElement>document.getElementById('playSpeed');
const seekBwdButton = <HTMLButtonElement>document.getElementById('seekBwdButton');
const seekFwdButton = <HTMLButtonElement>document.getElementById('seekFwdButton');
const currentDuration = <HTMLParagraphElement>document.getElementById('currentDuration');
const fullDuration = <HTMLParagraphElement>document.getElementById('fullDuration');
const playPrevButton = <HTMLButtonElement>document.getElementById('playPrevButton');
const playNextButton = <HTMLButtonElement>document.getElementById('playNextButton');
const loopButton = <HTMLButtonElement>document.getElementById('loopButton');
const volumeChanger = <HTMLInputElement>document.getElementById('volumeChanger');
const volumeIcon = <HTMLLabelElement>volumeChanger.previousElementSibling;
const msn = 'mediaSession' in navigator;
function updatePositionState() {
if (msn)
if ('setPositionState' in navigator.mediaSession)
navigator.mediaSession.setPositionState({
duration: audio.duration,
playbackRate: audio.playbackRate,
position: audio.currentTime,
});
}
playButton.onclick = function() {
if (!store.stream.id) return;
store.player.playbackState === 'playing' ?
audio.pause() :
audio.play();
}
let historyID: string | undefined = '';
let historyTimeoutId = 0;
audio.onplaying = function() {
playButton.classList.replace(playButton.className, 'ri-pause-circle-fill');
store.player.playbackState = 'playing';
const id = store.stream.id;
if (!store.streamHistory.includes(id))
store.streamHistory.push(id);
if (getSaved('history') === 'off')
return;
const firstElementInHistory = <HTMLElement | null>getCollection('history').firstElementChild;
if (firstElementInHistory?.dataset.id !== id)
historyTimeoutId = window.setTimeout(() => {
if (historyID === id) {
addToCollection('history', store.stream);
// just in case we are already in the history collection
if (
listAnchor.classList.contains('view') &&
params.get('collection') === 'history'
)
goTo('history');
}
}, 1e4);
}
audio.onpause = function() {
playButton.classList.replace('ri-pause-circle-fill', 'ri-play-circle-fill');
store.player.playbackState = 'paused';
clearTimeout(historyTimeoutId);
}
let isPlayable = false;
const playableCheckerID = setInterval(() => {
if (store.streamHistory.length || params.has('url') || params.has('text') || !params.has('s')) {
isPlayable = true;
clearInterval(playableCheckerID);
}
}, 500);
audio.onloadeddata = function() {
playButton.classList.replace('ri-loader-3-line', 'ri-play-circle-fill');
if (isPlayable) audio.play();
historyID = store.stream.id;
clearTimeout(historyTimeoutId);
// persist playback speed
if (playSpeed.value !== '1.00')
audio.playbackRate = parseFloat(playSpeed.value);
}
audio.onwaiting = function() {
playButton.classList.replace(playButton.className, 'ri-loader-3-line');
}
playSpeed.onchange = function() {
const speed = parseFloat(playSpeed.value);
if (speed < 0 || speed > 4)
return;
audio.playbackRate = speed;
updatePositionState();
playSpeed.blur();
}
seekFwdButton.onclick = function() {
audio.currentTime += 15;
updatePositionState();
}
seekBwdButton.onclick = function() {
audio.currentTime -= 15;
updatePositionState();
}
progress.onchange = function() {
const value = parseInt(progress.value);
if (value < 0 || value > audio.duration)
return;
audio.currentTime = value;
progress.blur();
}
audio.ontimeupdate = function() {
if (progress === document.activeElement)
return;
const seconds = Math.floor(audio.currentTime);
progress.value = seconds.toString();
currentDuration.textContent = convertSStoHHMMSS(seconds);
}
audio.onloadedmetadata = function() {
title.textContent = store.stream.title;
progress.value = '0';
progress.min = '0';
progress.max = Math.floor(audio.duration).toString();
fullDuration.textContent = convertSStoHHMMSS(audio.duration);
}
audio.oncanplaythrough = function() {
// prefetch beforehand to speed up experience
const nextItem = store.queue[0];
if (nextItem)
getData(nextItem, true);
}
audio.onerror = function() {
audio.pause();
const id = store.stream.id;
if (getSaved('custom_instance_2'))
return notify('Proxy failed to decrypt stream');
if (store.player.HLS) {
notify('PipedProxy failed to decrypt stream, Retrying...');
player(id);
return;
}
const origin = new URL(audio.src).origin;
title.textContent = 'Error 403, handling Error...';
if (store.api.index < store.api.invidious.length) {
const proxy = store.api.invidious[store.api.index];
audio.src = audio.src.replace(origin, proxy);
title.textContent = 'trying to load via Proxy ' + store.api.index;
store.api.index++;
}
else {
store.api.index = 0;
notify('Error 403 unauthenticated stream.');
title.textContent = store.stream.title;
getDownloadLink(store.actionsMenu.id)
.then(_ => {
if (_)
audio.src = _;
else throw new Error();
})
.catch(() => {
playButton.classList.replace(playButton.className, 'ri-stop-circle-fill');
})
}
}
loopButton.onclick = function() {
loopButton.classList.toggle('on');
audio.loop = !audio.loop;
}
playPrevButton.onclick = function() {
if (store.streamHistory.length > 1) {
appendToQueuelist(store.stream, true);
store.streamHistory.pop();
player(store.streamHistory[store.streamHistory.length - 1]);
}
}
function onEnd() {
playButton.classList.replace(playButton.className, 'ri-stop-circle-fill');
if (queuelist.childElementCount)
firstItemInQueue().click();
}
audio.onended = playNextButton.onclick = onEnd;
volumeIcon.onclick = function() {
volumeChanger.value = audio.volume ? '0' : '100';
audio.volume = audio.volume ? 0 : 1;
volumeIcon.classList.replace(
volumeIcon.className,
`ri-volume-${volumeIcon.className.includes('mute') ? 'up' : 'mute'
}-fill`
);
}
volumeChanger.oninput = function() {
audio.volume = parseFloat(volumeChanger.value) / 100;
audio.volume === 1 ?
removeSaved('volume') :
save('volume', volumeChanger.value);
volumeIcon.classList.replace(
volumeIcon.className,
audio.volume ?
`ri-volume-${audio.volume > 0.5 ? 'up' : 'down'}-fill` :
'ri-volume-mute-fill');
}
const savedVol = getSaved('volume');
if (savedVol) {
volumeChanger.value = savedVol;
audio.volume = parseFloat(volumeChanger.value) / 100;
}
if (msn) {
navigator.mediaSession.setActionHandler('play', () => {
audio.play();
});
navigator.mediaSession.setActionHandler('pause', () => {
audio.pause();
});
navigator.mediaSession.setActionHandler("seekforward", () => {
audio.currentTime += 15;
updatePositionState();
});
navigator.mediaSession.setActionHandler("seekbackward", () => {
audio.currentTime -= 15;
updatePositionState();
});
navigator.mediaSession.setActionHandler("seekto", e => {
audio.currentTime = e.seekTime || 0;
updatePositionState();
});
navigator.mediaSession.setActionHandler("nexttrack", () => {
onEnd();
updatePositionState();
});
}