UNPKG

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