UNPKG

@tarojs/taro-h5

Version:
102 lines (99 loc) 6.89 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.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; } }; /** * @TODO destroy得并不干净 */ this.destroy = () => { this.stop(); if (this.Instance) { this.Instance = undefined; } }; this.onCanplay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('canplay', 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.onStop = (callback = () => { }) => this.stopStack.add(callback); this.onEnded = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('ended', callback); }; this.onTimeUpdate = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('timeupdate', callback); }; this.onError = (callback) => this.errorStack.add(callback); this.onWaiting = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.addEventListener('waiting', 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.offCanplay = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('canplay', 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.offStop = (callback = () => { }) => this.stopStack.remove(callback); this.offEnded = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('ended', callback); }; this.offTimeUpdate = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('timeupdate', callback); }; this.offError = (callback = () => { }) => this.errorStack.remove(callback); this.offWaiting = (callback = () => { }) => { var _a; return (_a = this.Instance) === null || _a === void 0 ? void 0 : _a.removeEventListener('waiting', 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.Instance = new Audio(); this.errorStack = new CallbackManager(); this.stopStack = new CallbackManager(); this.Instance.onerror = this.errorStack.trigger; Taro.eventCenter.on('__taroRouterChange', () => { this.stop(); }); this.onPlay(() => { if (this.__isFirstPlay) { this.__isFirstPlay = false; this.seek(this.startTime); } }); } 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; } } } export { InnerAudioContext }; //# sourceMappingURL=InnerAudioContext.js.map