UNPKG

@tarojs/taro-h5

Version:
135 lines (132 loc) • 7.19 kB
import Taro from '@tarojs/api'; import { permanentlyNotSupport } from '../../../utils/index.js'; import { CallbackManager } from '../../../utils/handler.js'; class InnerAudioContext { constructor() { this.__startTime = 0; this.__isFirstPlay = true; this.listeners = new Map(); this.onRouterChange = () => this.stop(); this.onFirstPlay = () => { if (this.__isFirstPlay) { this.__isFirstPlay = false; this.seek(this.startTime); } }; 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.stop = () => { this.pause(); this.seek(0); this.stopStack.trigger(); }; this.seek = (position) => { if (this.Instance) { this.Instance.currentTime = position; } }; this.destroy = () => { if (!this.Instance) return; this.stop(); Taro.eventCenter.off('__taroRouterChange', this.onRouterChange); this.listeners.forEach((_, type) => this.removeListener(type)); this.Instance.removeEventListener('play', this.onFirstPlay); this.Instance.onerror = null; this.Instance.removeAttribute('src'); this.errorStack.clear(); this.stopStack.clear(); this.Instance = undefined; }; this.onCanplay = (callback = () => { }) => this.addListener('canplay', callback); this.onPlay = (callback = () => { }) => this.addListener('play', callback); this.onPause = (callback = () => { }) => this.addListener('pause', callback); this.onStop = (callback = () => { }) => this.stopStack.add(callback); this.onEnded = (callback = () => { }) => this.addListener('ended', callback); this.onTimeUpdate = (callback = () => { }) => this.addListener('timeupdate', callback); this.onError = (callback) => this.errorStack.add(callback); this.onWaiting = (callback = () => { }) => this.addListener('waiting', callback); this.onSeeking = (callback = () => { }) => this.addListener('seeking', callback); this.onSeeked = (callback = () => { }) => this.addListener('seeked', callback); this.offCanplay = (callback) => this.removeListener('canplay', callback); this.offPlay = (callback) => this.removeListener('play', callback); this.offPause = (callback) => this.removeListener('pause', callback); this.offStop = (callback) => this.stopStack.remove(callback); this.offEnded = (callback) => this.removeListener('ended', callback); this.offTimeUpdate = (callback) => this.removeListener('timeupdate', callback); this.offError = (callback) => this.errorStack.remove(callback); this.offWaiting = (callback) => this.removeListener('waiting', callback); this.offSeeking = (callback) => this.removeListener('seeking', callback); this.offSeeked = (callback) => this.removeListener('seeked', callback); this.Instance = new Audio(); this.errorStack = new CallbackManager(); this.stopStack = new CallbackManager(); this.Instance.onerror = this.errorStack.trigger; Taro.eventCenter.on('__taroRouterChange', this.onRouterChange); // Keep internal playback handling separate from removable user listeners. this.Instance.addEventListener('play', this.onFirstPlay); } set autoplay(e) { this.setProperty('autoplay', e); } get autoplay() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.autoplay) || 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; } get currentTime() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.currentTime) || 0; } set currentTime(e) { this.seek(e); } get duration() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.duration) || 0; } set loop(e) { this.setProperty('loop', e); } get loop() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.loop) || false; } get paused() { var _a, _b; return (_b = (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.paused) !== null && _b !== void 0 ? _b : true; } set src(e) { this.setProperty('src', e); } get src() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.src) || ''; } set volume(e) { this.setProperty('volume', e); } get volume() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.volume) || 0; } set playbackRate(e) { this.setProperty('playbackRate', e); } get playbackRate() { var _a; return ((_a = this.Instance) === null || _a === void 0 ? void 0 : _a.playbackRate) || 0; } set obeyMuteSwitch(_e) { permanentlyNotSupport('InnerAudioContext.obeyMuteSwitch')(); } get obeyMuteSwitch() { return true; } set startTime(e) { this.__startTime = e; } get startTime() { return this.__startTime || 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; } } addListener(type, callback) { if (!this.Instance) return; let callbacks = this.listeners.get(type); if (!callbacks) { callbacks = new Set(); this.listeners.set(type, callbacks); } callbacks.add(callback); this.Instance.addEventListener(type, callback); } removeListener(type, callback) { var _a; const callbacks = this.listeners.get(type); if (callback) { (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, callback); callbacks === null || callbacks === void 0 ? void 0 : callbacks.delete(callback); } else { callbacks === null || callbacks === void 0 ? void 0 : callbacks.forEach(listener => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener); }); callbacks === null || callbacks === void 0 ? void 0 : callbacks.clear(); } if (!(callbacks === null || callbacks === void 0 ? void 0 : callbacks.size)) this.listeners.delete(type); } } export { InnerAudioContext }; //# sourceMappingURL=InnerAudioContext.js.map