UNPKG

7 kBJavaScriptView Raw
1import { permanentlyNotSupport } from '../../../utils/index.js';
2import { CallbackManager } from '../../../utils/handler.js';
3
4class BackgroundAudioManager {
5 constructor() {
6 this.__startTime = 0;
7 this.play = () => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.play(); };
8 this.pause = () => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.pause(); };
9 this.seek = (position) => {
10 if (this.Instance) {
11 this.Instance.currentTime = position;
12 }
13 };
14 this.stop = () => {
15 this.pause();
16 this.seek(0);
17 this.stopStack.trigger();
18 };
19 this.onCanplay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('canplay', callback); };
20 this.onWaiting = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('waiting', callback); };
21 this.onError = (callback) => this.errorStack.add(callback);
22 this.onPlay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('play', callback); };
23 this.onPause = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('pause', callback); };
24 this.onSeeking = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('seeking', callback); };
25 this.onSeeked = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('seeked', callback); };
26 this.onEnded = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('ended', callback); };
27 this.onStop = (callback = () => { }) => this.stopStack.add(callback);
28 this.onTimeUpdate = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('timeupdate', callback); };
29 this.onPrev = permanentlyNotSupport('BackgroundAudioManager.onPrev');
30 this.onNext = permanentlyNotSupport('BackgroundAudioManager.onNext');
31 this.offCanplay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('canplay', callback); };
32 this.offWaiting = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('waiting', callback); };
33 this.offError = (callback = () => { }) => this.errorStack.remove(callback);
34 this.offPlay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('play', callback); };
35 this.offPause = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('pause', callback); };
36 this.offSeeking = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('seeking', callback); };
37 this.offSeeked = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('seeked', callback); };
38 this.offEnded = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('ended', callback); };
39 this.offStop = (callback = () => { }) => this.stopStack.remove(callback);
40 this.offTimeUpdate = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('timeupdate', callback); };
41 this.offPrev = permanentlyNotSupport('BackgroundAudioManager.offPrev');
42 this.offNext = permanentlyNotSupport('BackgroundAudioManager.offNext');
43 this.Instance = new Audio();
44 this.errorStack = new CallbackManager();
45 this.stopStack = new CallbackManager();
46 this.Instance.autoplay = true;
47 this.onPlay(() => {
48 if (this.currentTime !== this.startTime) {
49 this.seek(this.startTime);
50 }
51 });
52 }
53 set src(e) { this.setProperty('src', e); }
54 get src() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.src) || ''; }
55 set startTime(e) { this.__startTime = e; }
56 get startTime() { return this.__startTime || 0; }
57 set title(e) { this.dataset('title', e); }
58 get title() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.title) || ''; }
59 set epname(e) { this.dataset('epname', e); }
60 get epname() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.epname) || ''; }
61 set singer(e) { this.dataset('singer', e); }
62 get singer() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.singer) || ''; }
63 set coverImgUrl(e) { this.dataset('coverImgUrl', e); }
64 get coverImgUrl() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.coverImgUrl) || ''; }
65 set webUrl(e) { this.dataset('webUrl', e); }
66 get webUrl() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.webUrl) || ''; }
67 set protocol(e) { this.dataset('protocol', e); }
68 get protocol() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.protocol) || ''; }
69 set playbackRate(e) { this.setProperty('playbackRate', e); }
70 get playbackRate() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.playbackRate) || 0; }
71 get duration() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.duration) || 0; }
72 get currentTime() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.currentTime) || 0; }
73 get paused() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.paused) || false; }
74 get buffered() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.buffered.length) || 0; }
75 set referrerPolicy(e) { var _a; (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.setAttribute('referrerpolicy', e); }
76 get referrerPolicy() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.getAttribute('referrerpolicy')) || 'origin'; }
77 setProperty(key, value) {
78 if (this.Instance) {
79 this.Instance[key] = value;
80 }
81 }
82 dataset(key, value) {
83 if (this.Instance) {
84 this.Instance.dataset[key] = value;
85 }
86 }
87}
88
89export { BackgroundAudioManager };
90//# sourceMappingURL=BackgroundAudioManager.js.map