@realsee/vreo 0.2.0-alpha.12 → 0.2.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +1 -1
  2. package/docs/assets/search.js +1 -1
  3. package/docs/classes/Player.AudioLike.html +52 -0
  4. package/docs/classes/Player.Controller.html +3 -3
  5. package/docs/classes/Player.Player-1.html +3 -3
  6. package/docs/classes/Player.VideoAgentMesh.html +12 -12
  7. package/docs/classes/Player.VideoAgentScene.html +1 -1
  8. package/docs/demo/assets/AppReact.388465e9.js +1 -0
  9. package/docs/demo/assets/default.0350c336.css +1 -0
  10. package/docs/demo/assets/default.f03f1cdd.js +64 -0
  11. package/docs/demo/assets/dynamic.3f7479e4.js +1 -0
  12. package/docs/demo/assets/examples.02abaf01.js +25 -0
  13. package/docs/demo/assets/examples.dac4a6fe.css +1 -0
  14. package/docs/demo/assets/index.485f13f7.js +1 -0
  15. package/docs/demo/assets/index.a7bafdff.css +1 -0
  16. package/docs/demo/assets/main.0b6b41ab.js +1 -0
  17. package/docs/demo/assets/react.aa3cf7f7.css +1 -0
  18. package/docs/demo/assets/react.b14dadc7.js +25 -0
  19. package/docs/demo/assets/vendor.d3e0753c.js +3903 -0
  20. package/docs/demo/assets/znRoyv06SZQeqA7m.0057e8ae.js +1 -0
  21. package/docs/demo/examples.html +18 -0
  22. package/docs/demo/index-react-dynamic.html +21 -0
  23. package/docs/demo/index-react.html +21 -0
  24. package/docs/demo/index.html +20 -0
  25. package/docs/enums/Player.InfoPanelStyleEnum.html +1 -1
  26. package/docs/enums/Player.InfoPanelTypeEnum.html +1 -1
  27. package/docs/enums/Player.PanoEffectEnum.html +1 -1
  28. package/docs/enums/Player.PanoTagEnum.html +1 -1
  29. package/docs/enums/Player.PanoTagStyleEnum.html +2 -2
  30. package/docs/enums/Player.VreoKeyframeEnum.html +12 -12
  31. package/docs/enums/fivePlugins.CameraMovementEffect.html +1 -1
  32. package/docs/enums/fivePlugins.Rotation.html +1 -1
  33. package/docs/index.html +1 -1
  34. package/docs/interfaces/Player.CustomVreoKeyframeProps.html +1 -1
  35. package/docs/interfaces/Player.PlayerConfigs.html +2 -2
  36. package/docs/interfaces/Player.Vertex.html +1 -1
  37. package/docs/interfaces/Player.VideoAgentMeshOptions.html +1 -1
  38. package/docs/interfaces/Player.VreoKeyframe.html +1 -1
  39. package/docs/interfaces/Player.VreoUnit.html +1 -1
  40. package/docs/interfaces/Player.VreoVideo.html +1 -1
  41. package/docs/interfaces/fivePlugins.CameraMovementOptsCallback.html +1 -1
  42. package/docs/interfaces/fivePlugins.CameraMovementPluginExportType.html +2 -2
  43. package/docs/interfaces/fivePlugins.ModelTVVideoPluginData.html +1 -1
  44. package/docs/interfaces/fivePlugins.ModelTVVideoPluginExportType.html +1 -1
  45. package/docs/interfaces/fivePlugins.ModelTVVideoPluginParameterType.html +1 -1
  46. package/docs/interfaces/react.VreoActionCallbacks.html +6 -6
  47. package/docs/interfaces/react.VreoProviderProps.html +1 -1
  48. package/docs/modules/Player.html +16 -14
  49. package/docs/modules/custom.html +1 -1
  50. package/docs/modules/fivePlugins.html +2 -2
  51. package/docs/modules/react.html +1 -1
  52. package/lib/Player/Controller.d.ts +1 -1
  53. package/lib/Player/index.js +9 -15
  54. package/lib/Player/modules/VideoAgent/VideoAgentMesh.d.ts +4 -2
  55. package/lib/Player/modules/VideoAgent/VideoAgentMesh.js +67 -21
  56. package/lib/Player/modules/VideoAgent/VideoAgentScene.js +2 -1
  57. package/lib/shared-utils/AduioLike.d.ts +37 -0
  58. package/lib/shared-utils/AduioLike.js +165 -0
  59. package/package.json +2 -2
  60. package/resources/Player/Controller.ts +2 -0
  61. package/resources/Player/index.tsx +38 -28
  62. package/resources/Player/modules/VideoAgent/VideoAgentMesh.ts +37 -7
  63. package/resources/Player/modules/VideoAgent/VideoAgentScene.ts +2 -0
  64. package/resources/shared-utils/AduioLike.ts +108 -0
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = exports.AudioLike = void 0;
9
+
10
+ var _five = require("@realsee/five");
11
+
12
+ var _animationFrame = require("./animationFrame");
13
+
14
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
+
16
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
17
+
18
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
19
+
20
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
21
+
22
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
23
+
24
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
25
+
26
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
27
+
28
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
29
+
30
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
31
+
32
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
33
+
34
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
35
+
36
+ /**
37
+ * 模拟 `<Audio>` 功能:没有音频,但执行逻辑跟 `<Audio>` 相似。
38
+ */
39
+ var AudioLike = /*#__PURE__*/function (_Subscribe) {
40
+ _inherits(AudioLike, _Subscribe);
41
+
42
+ var _super = _createSuper(AudioLike);
43
+
44
+ function AudioLike() {
45
+ var _this;
46
+
47
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
48
+ duration = _ref.duration;
49
+
50
+ _classCallCheck(this, AudioLike);
51
+
52
+ _this = _super.call(this);
53
+
54
+ _defineProperty(_assertThisInitialized(_this), "$timestamp", 0);
55
+
56
+ _defineProperty(_assertThisInitialized(_this), "$currentTime", 0);
57
+
58
+ _defineProperty(_assertThisInitialized(_this), "$duration", 0);
59
+
60
+ _defineProperty(_assertThisInitialized(_this), "muted", false);
61
+
62
+ _defineProperty(_assertThisInitialized(_this), "src", '');
63
+
64
+ if (duration) {
65
+ _this.$duration = duration;
66
+ }
67
+
68
+ return _this;
69
+ }
70
+
71
+ _createClass(AudioLike, [{
72
+ key: "setAttribute",
73
+ value: function setAttribute(name, value) {// nothing todo
74
+ }
75
+ }, {
76
+ key: "play",
77
+ value: function play() {
78
+ var _this2 = this;
79
+
80
+ if (this.$timestamp === 0) {
81
+ this.$timestamp = performance.now();
82
+ }
83
+
84
+ if (this.$currentTime === this.$duration) {
85
+ this.$currentTime = 0;
86
+ }
87
+
88
+ this.stopInterval = (0, _animationFrame.requestAnimationFrameInterval)(function () {
89
+ return _this2.requestAnimationFrameLoop();
90
+ });
91
+ this.emit('play');
92
+ }
93
+ }, {
94
+ key: "pause",
95
+ value: function pause() {
96
+ if (this.stopInterval) {
97
+ this.stopInterval();
98
+ this.emit('pause');
99
+ this.stopInterval = undefined;
100
+ }
101
+
102
+ this.$timestamp = 0;
103
+ }
104
+ }, {
105
+ key: "requestAnimationFrameLoop",
106
+ value: function requestAnimationFrameLoop() {
107
+ var now = performance.now();
108
+ this.$currentTime = now - this.$timestamp;
109
+ this.emit('timeupdate');
110
+
111
+ if (this.$currentTime >= this.$duration - 10) {
112
+ this.$currentTime = this.$duration;
113
+ this.pause();
114
+ this.emit('ended');
115
+ }
116
+ }
117
+ }, {
118
+ key: "currentTime",
119
+ get: function get() {
120
+ return this.$currentTime / 1000;
121
+ },
122
+ set: function set(time) {
123
+ this.$currentTime = time;
124
+ }
125
+ }, {
126
+ key: "duration",
127
+ get: function get() {
128
+ return this.$duration;
129
+ },
130
+ set: function set(duration) {
131
+ this.pause();
132
+ this.$duration = duration;
133
+ this.$timestamp = 0;
134
+ this.$currentTime = 0;
135
+ }
136
+ }, {
137
+ key: "ended",
138
+ get: function get() {
139
+ return this.$currentTime === this.$duration;
140
+ }
141
+ }, {
142
+ key: "paused",
143
+ get: function get() {
144
+ return !this.stopInterval;
145
+ }
146
+ }, {
147
+ key: "addEventListener",
148
+ value: function addEventListener(evtName, callback) {
149
+ var useCapture = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
150
+ this.on(evtName, callback);
151
+ }
152
+ }, {
153
+ key: "removeEventListener",
154
+ value: function removeEventListener(evtName, callback) {
155
+ var useCapture = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
156
+ this.off(evtName, callback);
157
+ }
158
+ }]);
159
+
160
+ return AudioLike;
161
+ }(_five.Subscribe);
162
+
163
+ exports.AudioLike = AudioLike;
164
+ var _default = AudioLike;
165
+ exports["default"] = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realsee/vreo",
3
- "version": "0.2.0-alpha.12",
3
+ "version": "0.2.0-alpha.13",
4
4
  "description": "Vreo (VR Video 缩写) 是基于如视三维渲染引擎 Five 和 用户界面构建库 React 实现的如视 3D 空间剧本播放器。",
5
5
  "keywords": [
6
6
  "realsee",
@@ -21,7 +21,7 @@
21
21
  "dev": "vite --port=3088 --host 0.0.0.0",
22
22
  "build": "tsc && vite build",
23
23
  "preview": "vite preview",
24
- "docs": "npx typedoc",
24
+ "docs": "npx typedoc && yarn run build",
25
25
  "packages": "node ./dev-tools/build-packages.js"
26
26
  },
27
27
  "peerDependencies": {
@@ -114,12 +114,14 @@ export class Controller extends Subscribe<VreoKeyframeEvent> {
114
114
  this.videoInstance.pause()
115
115
  return
116
116
  }
117
+
117
118
  if (!this.playing) {
118
119
  if (!this.videoInstance?.paused) {
119
120
  this.videoInstance?.pause()
120
121
  }
121
122
  return
122
123
  }
124
+
123
125
  if (this.videoInstance?.paused && this.playing) {
124
126
  this.videoInstance.play()
125
127
  }
@@ -91,47 +91,56 @@ export class Player extends Subscribe<VreoKeyframeEvent> {
91
91
 
92
92
  // 预载逻辑
93
93
  // 是否降低图片分辨率
94
- if (this.$five.imageOptions.size && this.configs.imageOptions?.size && this.$five.imageOptions.size > this.configs.imageOptions.size) {
94
+ if (
95
+ this.$five.imageOptions.size &&
96
+ this.configs.imageOptions?.size &&
97
+ this.$five.imageOptions.size > this.configs.imageOptions.size
98
+ ) {
95
99
  this.$five.imageOptions.size = this.configs.imageOptions.size
96
100
  }
97
-
101
+
98
102
  // 预载数据中的点位
99
103
  if (preload || (preload === undefined && this.configs.autoPreload)) {
100
- const panoIndexMap = vreoUnit.keyframes.filter((vreoKeyframe) => {
101
- if (vreoKeyframe.type !== VreoKeyframeEnum.CameraMovement) {
102
- return false
103
- }
104
-
105
- const data = vreoKeyframe.data as CameraMovementData
106
- if (data.panoIndex === undefined) {
107
- return false
108
- }
109
- return true
110
- }).reduce((accu: Record<number, boolean>, curr) => {
111
- const panoIndex = curr.data.panoIndex as number
112
- if (!accu[panoIndex]) {
113
- accu[panoIndex] = true
114
- }
115
- return accu
116
- }, {})
117
-
104
+ const panoIndexMap = vreoUnit.keyframes
105
+ .filter((vreoKeyframe) => {
106
+ if (vreoKeyframe.type !== VreoKeyframeEnum.CameraMovement) {
107
+ return false
108
+ }
109
+
110
+ const data = vreoKeyframe.data as CameraMovementData
111
+ if (data.panoIndex === undefined) {
112
+ return false
113
+ }
114
+ return true
115
+ })
116
+ .reduce((accu: Record<number, boolean>, curr) => {
117
+ const panoIndex = curr.data.panoIndex as number
118
+ if (!accu[panoIndex]) {
119
+ accu[panoIndex] = true
120
+ }
121
+ return accu
122
+ }, {})
123
+
118
124
  const panoIndexs = Object.keys(panoIndexMap)
119
125
  for (let i = 0; i < panoIndexs.length; i++) {
120
126
  await this.$five.preloadPano(Number(panoIndexs[i]))
121
127
  }
122
128
  }
123
-
129
+
124
130
  // 新数据载入就绪
125
131
  this.emit('loaded', vreoUnit)
126
132
  this.controller.emit('loaded', vreoUnit)
127
133
 
128
- if (vreoUnit.video.url) {
129
- if (this.controller.videoAgentScene?.videoAgentMesh.videoInstance) {
130
- this.controller.videoAgentScene.videoAgentMesh.videoInstance.currentTime = currentTime / 1000
131
- }
132
- await this.controller.videoAgentScene?.videoAgentMesh.play(vreoUnit.video.url, currentTime / 1000)
133
- this.play()
134
+ if (this.controller.videoAgentScene?.videoAgentMesh.videoInstance) {
135
+ this.controller.videoAgentScene.videoAgentMesh.videoInstance.currentTime = currentTime / 1000
134
136
  }
137
+ await this.controller.videoAgentScene?.videoAgentMesh.play(
138
+ vreoUnit.video.url,
139
+ currentTime / 1000,
140
+ vreoUnit.video.duration
141
+ )
142
+ this.play()
143
+
135
144
  this.controller.run((type, keyframe) => this.emit(type, keyframe))
136
145
  return true
137
146
  }
@@ -143,9 +152,10 @@ export class Player extends Subscribe<VreoKeyframeEvent> {
143
152
  play(currentTime?: number) {
144
153
  if (this.controller.playing) return true
145
154
  if (currentTime && this.controller.videoInstance) {
146
- // Object.assign(window, {$vreoController: this.controller})
147
155
  this.controller.videoInstance.currentTime = currentTime / 1000
148
156
  }
157
+ Object.assign(window, {$vreoController: this.controller})
158
+
149
159
  this.controller.setPlaying(true)
150
160
  return true
151
161
  }
@@ -1,6 +1,7 @@
1
1
  import * as THREE from 'three'
2
2
  import { Preloader } from '../../../shared-utils/Preloader'
3
3
  import { makeObservable, observable, runInAction } from 'mobx'
4
+ import AudioLike from '../../../shared-utils/AduioLike'
4
5
 
5
6
  const vertexShader = `
6
7
  varying vec2 vUv;
@@ -76,14 +77,21 @@ export class VideoAgentMesh extends THREE.Mesh {
76
77
  freeze: boolean
77
78
  paused: boolean
78
79
  audioInstance: HTMLAudioElement
80
+ audioLikeInstance: AudioLike
79
81
  $removeEventListener: () => void
80
82
 
81
- get videoInstance(): HTMLAudioElement {
83
+ get videoInstance(): HTMLAudioElement | AudioLike {
84
+ if (!this.videoUrl) {
85
+ return this.audioLikeInstance
86
+ }
87
+
82
88
  if (this.videoUrl?.endsWith('mp3')) {
83
89
  return this.audioInstance
84
90
  }
91
+
85
92
  const uniforms = (this.material as THREE.ShaderMaterial).uniforms
86
93
  const videoInstance = uniforms.map.value.image as HTMLVideoElement
94
+
87
95
  return videoInstance
88
96
  }
89
97
 
@@ -155,23 +163,33 @@ export class VideoAgentMesh extends THREE.Mesh {
155
163
  this.audioInstance = options.audioInstance
156
164
  }
157
165
 
166
+ this.audioLikeInstance = new AudioLike()
167
+
158
168
  makeObservable(this, { paused: observable })
159
169
 
160
170
  const updatePaused = (paused: boolean) => runInAction(() => (this.paused = paused))
161
171
  const onPause = () => updatePaused(true)
162
172
  const onPlay = () => updatePaused(false)
163
173
 
164
- this.videoInstance.addEventListener('pause', onPause)
165
- this.videoInstance.addEventListener('play', onPlay)
174
+ this.audioInstance.addEventListener('pause', onPause)
175
+ this.audioInstance.addEventListener('play', onPlay)
176
+ this.options.videoInstance?.addEventListener('pause', onPause)
177
+ this.options.videoInstance?.addEventListener('play', onPlay)
178
+ this.audioLikeInstance.addEventListener('pause', onPause)
179
+ this.audioLikeInstance.addEventListener('play', onPlay)
166
180
 
167
181
  this.$removeEventListener = () => {
168
- this.videoInstance.removeEventListener('pause', onPause)
169
- this.videoInstance.removeEventListener('play', onPlay)
182
+ this.audioInstance.removeEventListener('pause', onPause)
183
+ this.audioInstance.removeEventListener('play', onPlay)
184
+ this.options.videoInstance?.removeEventListener('pause', onPause)
185
+ this.options.videoInstance?.removeEventListener('play', onPlay)
186
+ this.audioLikeInstance.removeEventListener('pause', onPause)
187
+ this.audioLikeInstance.removeEventListener('play', onPlay)
170
188
  }
171
189
  }
172
190
 
173
-
174
191
  private async update(videoUrl: string) {
192
+
175
193
  if (this.videoUrl === videoUrl) {
176
194
  return
177
195
  }
@@ -191,19 +209,31 @@ export class VideoAgentMesh extends THREE.Mesh {
191
209
  this.videoInstance.muted = false
192
210
  uniforms.enable.value = this.videoUrl?.endsWith('.mp4') ? 1 : 0
193
211
  this.videoInstance.removeEventListener('timeupdate', onStart, false)
212
+
194
213
  }
195
214
 
196
215
  this.videoInstance.addEventListener('timeupdate', onStart, false)
197
216
  }
198
217
 
199
- async play(videoUrl = '', currentTime = 0) {
218
+ async play(videoUrl = '', currentTime = 0, duration?: number) {
200
219
  videoUrl = videoUrl || ''
220
+ if (duration && !videoUrl) {
221
+ if (this.currentTime) {
222
+ this.videoInstance.currentTime = currentTime
223
+ }
224
+ (this.videoInstance as AudioLike).duration = duration
225
+ this.videoUrl = ''
226
+ this.videoInstance.play()
227
+ return true
228
+ }
229
+
201
230
  if (!videoUrl) {
202
231
  if (this.videoUrl) await this.videoInstance.play()
203
232
  else console.warn('警告:视频资源未初始化。')
204
233
  return true
205
234
  }
206
235
 
236
+ console.log('play', videoUrl, this.videoUrl)
207
237
  if (videoUrl === this.videoUrl) {
208
238
  this.videoInstance.currentTime = currentTime
209
239
  await this.videoInstance.play()
@@ -33,6 +33,8 @@ export class VideoAgentScene {
33
33
  run = () => {
34
34
  const domElement = this.renderer.domElement
35
35
 
36
+ // console.log('renderder', this.videoAgentMesh.paused, this.videoAgentMesh.freeze)
37
+
36
38
  // 视频开始 1s 不渲染:规避某些设备黑屏闪烁问题。视频前 2s 是最好是静默的。
37
39
  if (!(this.videoAgentMesh.paused || this.videoAgentMesh.freeze) && this.videoAgentMesh.currentTime > 1000) {
38
40
  this.renderer.render(this.scene, this.camera)
@@ -0,0 +1,108 @@
1
+ import { Subscribe } from '@realsee/five'
2
+ import { requestAnimationFrameInterval } from './animationFrame'
3
+
4
+ /**
5
+ * 模拟 `<Audio>` 事件。
6
+ */
7
+ export type AudioLikeEvent = {
8
+ // 播放
9
+ play: () => void
10
+ // 语音状态 播放 -> 暂停
11
+ pause: () => void
12
+ // 语音时间刷新
13
+ timeupdate: () => void
14
+ // 播放
15
+ ended: () => void
16
+ }
17
+
18
+
19
+ /**
20
+ * 模拟 `<Audio>` 功能:没有音频,但执行逻辑跟 `<Audio>` 相似。
21
+ */
22
+ export class AudioLike extends Subscribe<AudioLikeEvent> {
23
+ private $timestamp = 0
24
+ private $currentTime = 0
25
+ private $duration = 0
26
+ private stopInterval?: () => void
27
+ muted = false
28
+ src = ''
29
+
30
+ setAttribute(name: string, value: string) {
31
+ // nothing todo
32
+ }
33
+
34
+ constructor({ duration }: { duration?: number } = {}) {
35
+ super()
36
+ if (duration) {
37
+ this.$duration = duration
38
+ }
39
+ }
40
+
41
+ play() {
42
+ if (this.$timestamp === 0) {
43
+ this.$timestamp = performance.now()
44
+ }
45
+ if (this.$currentTime === this.$duration) {
46
+ this.$currentTime = 0
47
+ }
48
+ this.stopInterval = requestAnimationFrameInterval(() => this.requestAnimationFrameLoop())
49
+ this.emit('play')
50
+ }
51
+
52
+ pause() {
53
+ if (this.stopInterval) {
54
+ this.stopInterval()
55
+ this.emit('pause')
56
+ this.stopInterval = undefined
57
+ }
58
+ this.$timestamp = 0
59
+ }
60
+
61
+ requestAnimationFrameLoop() {
62
+ const now = performance.now()
63
+ this.$currentTime = now - this.$timestamp
64
+ this.emit('timeupdate')
65
+ if (this.$currentTime >= this.$duration - 10) {
66
+ this.$currentTime = this.$duration
67
+ this.pause()
68
+ this.emit('ended')
69
+ }
70
+ }
71
+
72
+ get currentTime() {
73
+ return this.$currentTime / 1000
74
+ }
75
+
76
+ set currentTime(time: number) {
77
+ this.$currentTime = time
78
+ }
79
+
80
+ get duration() {
81
+ return this.$duration
82
+ }
83
+
84
+ set duration(duration: number) {
85
+ this.pause()
86
+ this.$duration = duration
87
+ this.$timestamp = 0
88
+ this.$currentTime = 0
89
+ }
90
+
91
+ get ended() {
92
+ return this.$currentTime === this.$duration
93
+ }
94
+
95
+ get paused() {
96
+ return !this.stopInterval
97
+ }
98
+
99
+ addEventListener(evtName: keyof AudioLikeEvent, callback: () => void, useCapture = false) {
100
+ this.on(evtName, callback)
101
+ }
102
+
103
+ removeEventListener(evtName: keyof AudioLikeEvent, callback: () => void, useCapture = false) {
104
+ this.off(evtName, callback)
105
+ }
106
+ }
107
+
108
+ export default AudioLike