-
Notifications
You must be signed in to change notification settings - Fork 51
/
player.ts
85 lines (63 loc) · 2.2 KB
/
player.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
import { audio, favButton, favIcon, playButton } from "./dom";
import { convertSStoHHMMSS } from "./utils";
import { params, store, getSaved } from "./store";
import { setMetaData } from "../modules/setMetadata";
import { getDB } from "./libraryUtils";
import { getData } from "../modules/getStreamData";
export default async function player(id: string | null = '') {
if (!id) return;
playButton.classList.replace(playButton.className, 'ri-loader-3-line');
const data = await getData(id);
if (data && 'audioStreams' in data)
store.player.data = data;
else return player(id);
await setMetaData({
id: id,
title: data.title,
author: data.uploader,
duration: convertSStoHHMMSS(data.duration),
channelUrl: data.uploaderUrl
});
if (store.player.legacy) {
audio.src = data.hls;
audio.load();
}
else {
const h = store.player.HLS;
h ?
h.loadSource(data.hls) :
import('../modules/setAudioStreams').then(mod => mod.setAudioStreams(
data.audioStreams
.sort((a: { bitrate: string }, b: { bitrate: string }) => (parseInt(a.bitrate) - parseInt(b.bitrate))
),
data.category === 'Music',
data.livestream
));
}
import('../modules/setSubtitles')
.then(mod => mod.setSubtitles(data.subtitles));
params.set('s', id);
if (location.pathname === '/')
history.replaceState({}, '', location.origin + '?s=' + params.get('s'));
if (getSaved('enqueueRelatedStreams') === 'on')
import('../modules/enqueueRelatedStreams')
.then(mod => mod.enqueueRelatedStreams(data.relatedStreams as StreamItem[]));
// favbutton reset
if (favButton.checked) {
favButton.checked = false;
favIcon.classList.remove('ri-heart-fill');
}
// favbutton set
if (getDB().favorites?.hasOwnProperty(id)) {
favButton.checked = true;
favIcon.classList.add('ri-heart-fill');
}
// related streams imported into discovery after 1min 40seconds, short streams are naturally filtered out
if (getSaved('discover') !== 'off')
import('../modules/setDiscoveries')
.then(mod => {
setTimeout(() => {
mod.setDiscoveries(id, data.relatedStreams as StreamItem[]);
}, 1e5);
});
}