@tarojs/taro-h5
Version:
Taro h5 framework
102 lines (99 loc) • 7.32 kB
JavaScript
import { permanentlyNotSupport } from '../../../utils/index.js';
import { CallbackManager } from '../../../utils/handler.js';
class BackgroundAudioManager {
constructor() {
this.__startTime = 0;
this.play = () => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.play(); };
this.pause = () => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.pause(); };
this.seek = (position) => {
if (this.Instance) {
this.Instance.currentTime = position;
}
};
this.stop = () => {
this.pause();
this.seek(0);
this.stopStack.trigger();
};
this.onCanplay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('canplay', callback); };
this.onWaiting = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('waiting', callback); };
this.onError = (callback) => this.errorStack.add(callback);
this.onPlay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('play', callback); };
this.onPause = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('pause', callback); };
this.onSeeking = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('seeking', callback); };
this.onSeeked = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('seeked', callback); };
this.onEnded = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('ended', callback); };
this.onStop = (callback = () => { }) => this.stopStack.add(callback);
this.onTimeUpdate = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('timeupdate', callback); };
this.onPrev = permanentlyNotSupport('BackgroundAudioManager.onPrev');
this.onNext = permanentlyNotSupport('BackgroundAudioManager.onNext');
this.offCanplay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('canplay', callback); };
this.offWaiting = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('waiting', callback); };
this.offError = (callback = () => { }) => this.errorStack.remove(callback);
this.offPlay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('play', callback); };
this.offPause = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('pause', callback); };
this.offSeeking = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('seeking', callback); };
this.offSeeked = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('seeked', callback); };
this.offEnded = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('ended', callback); };
this.offStop = (callback = () => { }) => this.stopStack.remove(callback);
this.offTimeUpdate = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('timeupdate', callback); };
this.offPrev = permanentlyNotSupport('BackgroundAudioManager.offPrev');
this.offNext = permanentlyNotSupport('BackgroundAudioManager.offNext');
this.Instance = new Audio();
this.errorStack = new CallbackManager();
this.stopStack = new CallbackManager();
this.Instance.onerror = this.errorStack.trigger;
this.Instance.autoplay = true;
this.onPlay(() => {
if (this.currentTime !== this.startTime) {
this.seek(this.startTime);
}
});
}
set src(e) { this.setProperty('src', e); }
get src() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.src) || ''; }
set startTime(e) { this.__startTime = e; }
get startTime() { return this.__startTime || 0; }
set title(e) { this.dataset('title', e); }
get title() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.title) || ''; }
set epname(e) { this.dataset('epname', e); }
get epname() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.epname) || ''; }
set singer(e) { this.dataset('singer', e); }
get singer() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.singer) || ''; }
set coverImgUrl(e) { this.dataset('coverImgUrl', e); }
get coverImgUrl() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.coverImgUrl) || ''; }
set webUrl(e) { this.dataset('webUrl', e); }
get webUrl() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.webUrl) || ''; }
set protocol(e) { this.dataset('protocol', e); }
get protocol() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.dataset.protocol) || ''; }
set playbackRate(e) { this.setProperty('playbackRate', e); }
get playbackRate() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.playbackRate) || 0; }
get duration() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.duration) || 0; }
get currentTime() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.currentTime) || 0; }
get paused() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.paused) || false; }
get buffered() {
const { currentTime = 0, buffered: timeRange } = this.Instance || {};
if (timeRange) {
for (let i = 0; i < timeRange.length; i++) {
if (timeRange.start(i) <= currentTime && timeRange.end(i) >= currentTime) {
return timeRange.end(i);
}
}
}
return 0;
}
set referrerPolicy(e) { var _a; (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.setAttribute('referrerpolicy', e); }
get referrerPolicy() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.getAttribute('referrerpolicy')) || 'origin'; }
setProperty(key, value) {
if (this.Instance) {
this.Instance[key] = value;
}
}
dataset(key, value) {
if (this.Instance) {
this.Instance.dataset[key] = value;
}
}
}
export { BackgroundAudioManager };
//# sourceMappingURL=BackgroundAudioManager.js.map