@clockworkdog/cogs-client 3.0.0-alpha.2 → 3.0.0-alpha.4
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.
- package/dist/AudioPlayer.d.ts +49 -0
- package/dist/AudioPlayer.js +473 -0
- package/dist/VideoPlayer.d.ts +49 -0
- package/dist/VideoPlayer.js +381 -0
- package/dist/browser/index.mjs +2542 -1550
- package/dist/browser/index.umd.js +13 -13
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/types/MediaSchema.d.ts +13 -0
- package/dist/types/MediaSchema.js +3 -0
- package/dist/types/VideoState.d.ts +26 -0
- package/dist/types/VideoState.js +5 -0
- package/package.json +2 -2
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import CogsConnection from './CogsConnection';
|
|
2
|
+
import { AudioState } from './types/AudioState';
|
|
3
|
+
import MediaClipStateMessage from './types/MediaClipStateMessage';
|
|
4
|
+
type EventTypes = {
|
|
5
|
+
state: AudioState;
|
|
6
|
+
audioClipState: MediaClipStateMessage;
|
|
7
|
+
};
|
|
8
|
+
export default class AudioPlayer {
|
|
9
|
+
private cogsConnection;
|
|
10
|
+
private eventTarget;
|
|
11
|
+
private globalVolume;
|
|
12
|
+
private audioClipPlayers;
|
|
13
|
+
private sinkId;
|
|
14
|
+
constructor(cogsConnection: CogsConnection<any>);
|
|
15
|
+
setGlobalVolume(volume: number): void;
|
|
16
|
+
playAudioClip(path: string, { playId, volume, fade, loop }: {
|
|
17
|
+
playId: string;
|
|
18
|
+
volume: number;
|
|
19
|
+
fade?: number;
|
|
20
|
+
loop: boolean;
|
|
21
|
+
}): void;
|
|
22
|
+
pauseAudioClip(path: string, { fade }: {
|
|
23
|
+
fade?: number;
|
|
24
|
+
}, onlySoundId?: number, allowIfPauseRequested?: boolean): void;
|
|
25
|
+
stopAudioClip(path: string, { fade }: {
|
|
26
|
+
fade?: number;
|
|
27
|
+
}, onlySoundId?: number, allowIfStopRequested?: boolean): void;
|
|
28
|
+
stopAllAudioClips(options: {
|
|
29
|
+
fade?: number;
|
|
30
|
+
}): void;
|
|
31
|
+
setAudioClipVolume(path: string, { volume, fade }: {
|
|
32
|
+
volume: number;
|
|
33
|
+
fade?: number;
|
|
34
|
+
}): void;
|
|
35
|
+
private handleStoppedClip;
|
|
36
|
+
private updateActiveAudioClip;
|
|
37
|
+
private updateAudioClipPlayer;
|
|
38
|
+
setAudioSink(sinkId: string): void;
|
|
39
|
+
private updateConfig;
|
|
40
|
+
private notifyStateListeners;
|
|
41
|
+
private notifyClipStateListeners;
|
|
42
|
+
addEventListener<EventName extends keyof EventTypes>(type: EventName, listener: (ev: CustomEvent<EventTypes[EventName]>) => void, options?: boolean | AddEventListenerOptions): void;
|
|
43
|
+
removeEventListener<EventName extends keyof EventTypes>(type: EventName, listener: (ev: CustomEvent<EventTypes[EventName]>) => void, options?: boolean | EventListenerOptions): void;
|
|
44
|
+
private dispatchEvent;
|
|
45
|
+
private createPlayer;
|
|
46
|
+
private createClip;
|
|
47
|
+
private updatedClip;
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
2
|
+
/// <reference path="./types/howler.d.ts" />
|
|
3
|
+
import { Howl, Howler } from 'howler/dist/howler.core.min.js';
|
|
4
|
+
const DEBUG = false;
|
|
5
|
+
// Check an iOS-only property (See https://developer.mozilla.org/en-US/docs/Web/API/Navigator#non-standard_properties)
|
|
6
|
+
const IS_IOS = typeof navigator !== 'undefined' && typeof navigator.standalone !== 'undefined';
|
|
7
|
+
export default class AudioPlayer {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
constructor(cogsConnection) {
|
|
10
|
+
this.cogsConnection = cogsConnection;
|
|
11
|
+
this.eventTarget = new EventTarget();
|
|
12
|
+
this.globalVolume = 1;
|
|
13
|
+
this.audioClipPlayers = {};
|
|
14
|
+
this.sinkId = '';
|
|
15
|
+
// Send the current status of each clip to COGS
|
|
16
|
+
this.addEventListener('audioClipState', ({ detail }) => {
|
|
17
|
+
cogsConnection.sendMediaClipState(detail);
|
|
18
|
+
});
|
|
19
|
+
// Listen for audio control messages
|
|
20
|
+
cogsConnection.addEventListener('message', ({ message }) => {
|
|
21
|
+
switch (message.type) {
|
|
22
|
+
case 'media_config_update':
|
|
23
|
+
if (this.globalVolume !== message.globalVolume) {
|
|
24
|
+
this.setGlobalVolume(message.globalVolume);
|
|
25
|
+
}
|
|
26
|
+
if (message.audioOutput !== undefined) {
|
|
27
|
+
const sinkId = cogsConnection.getAudioSinkId(message.audioOutput);
|
|
28
|
+
this.setAudioSink(sinkId ?? '');
|
|
29
|
+
}
|
|
30
|
+
this.updateConfig(message.files);
|
|
31
|
+
break;
|
|
32
|
+
case 'audio_play':
|
|
33
|
+
this.playAudioClip(message.file, {
|
|
34
|
+
playId: message.playId,
|
|
35
|
+
volume: message.volume,
|
|
36
|
+
loop: Boolean(message.loop),
|
|
37
|
+
fade: message.fade,
|
|
38
|
+
});
|
|
39
|
+
break;
|
|
40
|
+
case 'audio_pause':
|
|
41
|
+
this.pauseAudioClip(message.file, { fade: message.fade });
|
|
42
|
+
break;
|
|
43
|
+
case 'audio_stop':
|
|
44
|
+
if (message.file) {
|
|
45
|
+
this.stopAudioClip(message.file, { fade: message.fade });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.stopAllAudioClips({ fade: message.fade });
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
case 'audio_set_clip_volume':
|
|
52
|
+
this.setAudioClipVolume(message.file, { volume: message.volume, fade: message.fade });
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
// On connection, send the current playing state of all clips
|
|
57
|
+
// (Usually empty unless websocket is reconnecting)
|
|
58
|
+
const sendInitialClipStates = () => {
|
|
59
|
+
const files = Object.entries(this.audioClipPlayers).map(([file, player]) => {
|
|
60
|
+
const activeClips = Object.values(player.activeClips);
|
|
61
|
+
const status = activeClips.some(({ state }) => state.type === 'playing' ||
|
|
62
|
+
state.type === 'pausing' ||
|
|
63
|
+
state.type === 'stopping' ||
|
|
64
|
+
state.type === 'play_requested' ||
|
|
65
|
+
state.type === 'pause_requested' ||
|
|
66
|
+
state.type === 'stop_requested')
|
|
67
|
+
? 'playing'
|
|
68
|
+
: activeClips.some(({ state }) => state.type === 'paused')
|
|
69
|
+
? 'paused'
|
|
70
|
+
: 'stopped';
|
|
71
|
+
return [file, status];
|
|
72
|
+
});
|
|
73
|
+
cogsConnection.sendInitialMediaClipStates({ mediaType: 'audio', files });
|
|
74
|
+
};
|
|
75
|
+
cogsConnection.addEventListener('open', sendInitialClipStates);
|
|
76
|
+
sendInitialClipStates();
|
|
77
|
+
}
|
|
78
|
+
setGlobalVolume(volume) {
|
|
79
|
+
this.globalVolume = volume;
|
|
80
|
+
Howler.volume(volume);
|
|
81
|
+
this.notifyStateListeners();
|
|
82
|
+
}
|
|
83
|
+
playAudioClip(path, { playId, volume, fade, loop }) {
|
|
84
|
+
log('Playing clip', { path });
|
|
85
|
+
if (!(path in this.audioClipPlayers)) {
|
|
86
|
+
log('Creating ephemeral clip', { path });
|
|
87
|
+
this.audioClipPlayers[path] = this.createClip(path, { preload: false, ephemeral: true });
|
|
88
|
+
}
|
|
89
|
+
this.updateAudioClipPlayer(path, (clipPlayer) => {
|
|
90
|
+
// Paused clips need to be played again
|
|
91
|
+
const pausedSoundIds = Object.entries(clipPlayer.activeClips)
|
|
92
|
+
.filter(([, { state }]) => state.type === 'paused')
|
|
93
|
+
.map(([id]) => parseInt(id));
|
|
94
|
+
const pausingSoundIds = Object.entries(clipPlayer.activeClips)
|
|
95
|
+
.filter(([, { state }]) => state.type === 'pausing')
|
|
96
|
+
.map(([id]) => parseInt(id));
|
|
97
|
+
pausedSoundIds.forEach((soundId) => {
|
|
98
|
+
log('Resuming paused clip', soundId);
|
|
99
|
+
clipPlayer.player.play(soundId);
|
|
100
|
+
});
|
|
101
|
+
// Clips with pause requested no longer need to pause, they can continue playing now
|
|
102
|
+
const pauseRequestedSoundIds = Object.entries(clipPlayer.activeClips)
|
|
103
|
+
.filter(([, { state }]) => state.type === 'pause_requested')
|
|
104
|
+
.map(([id]) => parseInt(id));
|
|
105
|
+
// If no currently paused/pausing/pause_requested clips, play a new clip
|
|
106
|
+
const newSoundIds = pausedSoundIds.length > 0 || pausingSoundIds.length > 0 || pauseRequestedSoundIds.length > 0 ? [] : [clipPlayer.player.play()];
|
|
107
|
+
// Pausing clips are technically currently playing as far as Howler is concerned
|
|
108
|
+
pausingSoundIds.forEach((soundId) => {
|
|
109
|
+
log('Stopping fade and resuming pausing clip', soundId);
|
|
110
|
+
// Stop the fade callback
|
|
111
|
+
clipPlayer.player.off('fade', undefined, soundId);
|
|
112
|
+
// Set loop property
|
|
113
|
+
clipPlayer.player.loop(loop, soundId);
|
|
114
|
+
// Update state to 'playing'
|
|
115
|
+
this.updateActiveAudioClip(path, soundId, (clip) => ({ ...clip, state: { type: 'playing' } }));
|
|
116
|
+
// Set volume, or start a new fade
|
|
117
|
+
if (isFadeValid(fade)) {
|
|
118
|
+
// Start fade when clip starts
|
|
119
|
+
fadeAudioPlayerVolume(clipPlayer.player, volume, fade * 1000, soundId);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
setAudioPlayerVolume(clipPlayer.player, volume, soundId);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
// paused and pause_requested clips treated the same, they should have their properties
|
|
126
|
+
// updated with the latest play action's properties
|
|
127
|
+
[...pausedSoundIds, ...pauseRequestedSoundIds, ...newSoundIds].forEach((soundId) => {
|
|
128
|
+
clipPlayer.player.loop(loop, soundId);
|
|
129
|
+
// Cleanup any old callbacks first
|
|
130
|
+
clipPlayer.player.off('play', undefined, soundId);
|
|
131
|
+
clipPlayer.player.off('pause', undefined, soundId);
|
|
132
|
+
clipPlayer.player.off('fade', undefined, soundId);
|
|
133
|
+
clipPlayer.player.off('end', undefined, soundId);
|
|
134
|
+
clipPlayer.player.off('stop', undefined, soundId);
|
|
135
|
+
// Non-preloaded clips don't yet have an HTML audio node
|
|
136
|
+
// so we need to set the audio output when it's playing
|
|
137
|
+
clipPlayer.player.once('play', () => {
|
|
138
|
+
setPlayerSinkId(clipPlayer.player, this.sinkId);
|
|
139
|
+
});
|
|
140
|
+
clipPlayer.player.once('stop', () => this.handleStoppedClip(path, playId, soundId), soundId);
|
|
141
|
+
// Looping clips fire the 'end' callback on every loop
|
|
142
|
+
clipPlayer.player.on('end', () => {
|
|
143
|
+
if (!clipPlayer.activeClips[soundId]?.loop) {
|
|
144
|
+
this.handleStoppedClip(path, playId, soundId);
|
|
145
|
+
}
|
|
146
|
+
}, soundId);
|
|
147
|
+
const activeClip = {
|
|
148
|
+
playId,
|
|
149
|
+
state: { type: 'play_requested' },
|
|
150
|
+
loop,
|
|
151
|
+
volume,
|
|
152
|
+
};
|
|
153
|
+
log('CLIP -> play_requested', soundId);
|
|
154
|
+
// Once clip starts, check if it should actually be paused or stopped
|
|
155
|
+
// If not, then update state to 'playing'
|
|
156
|
+
clipPlayer.player.once('play', () => {
|
|
157
|
+
const clipState = clipPlayer.activeClips[soundId]?.state;
|
|
158
|
+
if (clipState?.type === 'pause_requested') {
|
|
159
|
+
log('Clip started playing but should be paused', { path, soundId });
|
|
160
|
+
this.pauseAudioClip(path, { fade: clipState.fade }, soundId, true);
|
|
161
|
+
}
|
|
162
|
+
else if (clipState?.type === 'stop_requested') {
|
|
163
|
+
log('Clip started playing but should be stopped', { path, soundId });
|
|
164
|
+
this.stopAudioClip(path, { fade: clipState.fade }, soundId, true);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
log('CLIP -> playing', soundId);
|
|
168
|
+
this.updateActiveAudioClip(path, soundId, (clip) => ({ ...clip, state: { type: 'playing' } }));
|
|
169
|
+
}
|
|
170
|
+
}, soundId);
|
|
171
|
+
// To fade or to no fade?
|
|
172
|
+
if (isFadeValid(fade)) {
|
|
173
|
+
// Start fade when clip starts
|
|
174
|
+
clipPlayer.player.volume(0, soundId);
|
|
175
|
+
clipPlayer.player.mute(false, soundId);
|
|
176
|
+
clipPlayer.player.once('play', () => {
|
|
177
|
+
fadeAudioPlayerVolume(clipPlayer.player, volume, fade * 1000, soundId);
|
|
178
|
+
}, soundId);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
setAudioPlayerVolume(clipPlayer.player, volume, soundId);
|
|
182
|
+
}
|
|
183
|
+
// Track new/updated active clip
|
|
184
|
+
clipPlayer.activeClips = { ...clipPlayer.activeClips, [soundId]: activeClip };
|
|
185
|
+
});
|
|
186
|
+
return clipPlayer;
|
|
187
|
+
});
|
|
188
|
+
this.notifyClipStateListeners(playId, path, 'playing');
|
|
189
|
+
}
|
|
190
|
+
pauseAudioClip(path, { fade }, onlySoundId, allowIfPauseRequested) {
|
|
191
|
+
// No active clips to pause
|
|
192
|
+
if (Object.keys(this.audioClipPlayers[path]?.activeClips ?? {}).length === 0) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
this.updateAudioClipPlayer(path, (clipPlayer) => {
|
|
196
|
+
clipPlayer.activeClips = Object.fromEntries(Object.entries(clipPlayer.activeClips).map(([soundIdStr, clip]) => {
|
|
197
|
+
const soundId = parseInt(soundIdStr);
|
|
198
|
+
// If onlySoundId specified, only update that clip
|
|
199
|
+
if (onlySoundId === undefined || onlySoundId === soundId) {
|
|
200
|
+
if ((allowIfPauseRequested && clip.state.type === 'pause_requested') || clip.state.type === 'playing' || clip.state.type === 'pausing') {
|
|
201
|
+
if (isFadeValid(fade)) {
|
|
202
|
+
// Fade then pause
|
|
203
|
+
clipPlayer.player.once('fade', (soundId) => {
|
|
204
|
+
clipPlayer.player.pause(soundId);
|
|
205
|
+
log('CLIP -> paused (after fade)', soundId);
|
|
206
|
+
this.updateActiveAudioClip(path, soundId, (clip) => ({ ...clip, state: { type: 'paused' } }));
|
|
207
|
+
this.notifyClipStateListeners(clip.playId, path, 'paused');
|
|
208
|
+
}, soundId);
|
|
209
|
+
fadeAudioPlayerVolume(clipPlayer.player, 0, fade * 1000, soundId);
|
|
210
|
+
log('CLIP -> pausing', soundId);
|
|
211
|
+
clip.state = { type: 'pausing' };
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
// Pause now
|
|
215
|
+
clipPlayer.player.pause(soundId);
|
|
216
|
+
log('CLIP -> paused', soundId);
|
|
217
|
+
clip.state = { type: 'paused' };
|
|
218
|
+
this.notifyClipStateListeners(clip.playId, path, 'paused');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// Clip hasn't started playing yet, or has already had pause_requested (but fade may have changed so update here)
|
|
222
|
+
else if (clip.state.type === 'play_requested' || clip.state.type === 'pause_requested') {
|
|
223
|
+
log('CLIP -> pause_requested', soundId);
|
|
224
|
+
clip.state = { type: 'pause_requested', fade };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return [soundIdStr, clip];
|
|
228
|
+
}));
|
|
229
|
+
return clipPlayer;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
stopAudioClip(path, { fade }, onlySoundId, allowIfStopRequested) {
|
|
233
|
+
log('Stop audio clip', { activeClips: this.audioClipPlayers[path]?.activeClips });
|
|
234
|
+
// No active clips to stop
|
|
235
|
+
if (Object.keys(this.audioClipPlayers[path]?.activeClips ?? {}).length === 0) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.updateAudioClipPlayer(path, (clipPlayer) => {
|
|
239
|
+
clipPlayer.activeClips = Object.fromEntries(Object.entries(clipPlayer.activeClips).map(([soundIdStr, clip]) => {
|
|
240
|
+
const soundId = parseInt(soundIdStr);
|
|
241
|
+
// If onlySoundId specified, only update that clip
|
|
242
|
+
if (onlySoundId === undefined || onlySoundId === soundId) {
|
|
243
|
+
if ((allowIfStopRequested && clip.state.type === 'stop_requested') ||
|
|
244
|
+
clip.state.type === 'playing' ||
|
|
245
|
+
clip.state.type === 'pausing' ||
|
|
246
|
+
clip.state.type === 'paused' ||
|
|
247
|
+
clip.state.type === 'stopping') {
|
|
248
|
+
if (isFadeValid(fade) && clip.state.type !== 'paused') {
|
|
249
|
+
// Cleanup any old fade callbacks first
|
|
250
|
+
// TODO: Remove cast once https://github.com/DefinitelyTyped/DefinitelyTyped/pull/59411 is merged
|
|
251
|
+
clipPlayer.player.off('fade', soundId);
|
|
252
|
+
fadeAudioPlayerVolume(clipPlayer.player, 0, fade * 1000, soundId);
|
|
253
|
+
// Set callback after starting new fade, otherwise it will fire straight away as the previous fade is cancelled
|
|
254
|
+
clipPlayer.player.once('fade', (soundId) => {
|
|
255
|
+
clipPlayer.player.loop(false, soundId);
|
|
256
|
+
clipPlayer.player.stop(soundId);
|
|
257
|
+
}, soundId);
|
|
258
|
+
log('CLIP -> stopping', soundId);
|
|
259
|
+
clip.state = { type: 'stopping' };
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
log('Stop clip', soundId);
|
|
263
|
+
clipPlayer.player.loop(false, soundId);
|
|
264
|
+
clipPlayer.player.stop(soundId);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// Clip hasn't started playing yet, or has already had stop_requested (but fade may have changed so update here)
|
|
268
|
+
// or has pause_requested, but stop takes precedence
|
|
269
|
+
else if (clip.state.type === 'play_requested' || clip.state.type === 'pause_requested' || clip.state.type === 'stop_requested') {
|
|
270
|
+
log("Trying to stop clip which hasn't started playing yet", { path, soundId });
|
|
271
|
+
log('CLIP -> stop_requested', soundId);
|
|
272
|
+
clip.state = { type: 'stop_requested', fade };
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return [soundIdStr, clip];
|
|
276
|
+
}));
|
|
277
|
+
return clipPlayer;
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
stopAllAudioClips(options) {
|
|
281
|
+
log('Stopping all clips');
|
|
282
|
+
Object.keys(this.audioClipPlayers).forEach((path) => {
|
|
283
|
+
this.stopAudioClip(path, options);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
setAudioClipVolume(path, { volume, fade }) {
|
|
287
|
+
if (!(volume >= 0 && volume <= 1)) {
|
|
288
|
+
console.warn('Invalid volume', volume);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
// No active clips to set volume for
|
|
292
|
+
if (Object.keys(this.audioClipPlayers[path]?.activeClips ?? {}).length === 0) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
this.updateAudioClipPlayer(path, (clipPlayer) => {
|
|
296
|
+
clipPlayer.activeClips = Object.fromEntries(Object.entries(clipPlayer.activeClips).map(([soundIdStr, clip]) => {
|
|
297
|
+
// Ignored for pausing/stopping instances
|
|
298
|
+
if (clip.state.type !== 'pausing' && clip.state.type !== 'stopping') {
|
|
299
|
+
const soundId = parseInt(soundIdStr);
|
|
300
|
+
if (isFadeValid(fade)) {
|
|
301
|
+
fadeAudioPlayerVolume(clipPlayer.player, volume, fade * 1000, soundId);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
setAudioPlayerVolume(clipPlayer.player, volume, soundId);
|
|
305
|
+
}
|
|
306
|
+
return [soundIdStr, { ...clip, volume }];
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
return [soundIdStr, clip];
|
|
310
|
+
}
|
|
311
|
+
}));
|
|
312
|
+
return clipPlayer;
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
handleStoppedClip(path, playId, soundId) {
|
|
316
|
+
this.updateAudioClipPlayer(path, (clipPlayer) => {
|
|
317
|
+
delete clipPlayer.activeClips[soundId];
|
|
318
|
+
return clipPlayer;
|
|
319
|
+
});
|
|
320
|
+
this.notifyClipStateListeners(playId, path, 'stopped');
|
|
321
|
+
}
|
|
322
|
+
updateActiveAudioClip(path, soundId, update) {
|
|
323
|
+
this.updateAudioClipPlayer(path, (clipPlayer) => soundId in clipPlayer.activeClips
|
|
324
|
+
? { ...clipPlayer, activeClips: { ...clipPlayer.activeClips, [soundId]: update(clipPlayer.activeClips[soundId]) } }
|
|
325
|
+
: clipPlayer);
|
|
326
|
+
}
|
|
327
|
+
updateAudioClipPlayer(path, update) {
|
|
328
|
+
if (path in this.audioClipPlayers) {
|
|
329
|
+
this.audioClipPlayers = { ...this.audioClipPlayers, [path]: update(this.audioClipPlayers[path]) };
|
|
330
|
+
}
|
|
331
|
+
// Once last instance of an ephemeral clip is removed, cleanup and remove the player
|
|
332
|
+
const clipPlayer = this.audioClipPlayers[path];
|
|
333
|
+
if (clipPlayer && Object.keys(clipPlayer.activeClips ?? {}).length === 0 && clipPlayer.config.ephemeral) {
|
|
334
|
+
clipPlayer.player.unload();
|
|
335
|
+
delete this.audioClipPlayers[path];
|
|
336
|
+
}
|
|
337
|
+
this.notifyStateListeners();
|
|
338
|
+
}
|
|
339
|
+
setAudioSink(sinkId) {
|
|
340
|
+
log(`Setting sink ID for all clips:`, sinkId);
|
|
341
|
+
for (const clipPlayer of Object.values(this.audioClipPlayers)) {
|
|
342
|
+
setPlayerSinkId(clipPlayer.player, sinkId);
|
|
343
|
+
}
|
|
344
|
+
this.sinkId = sinkId;
|
|
345
|
+
}
|
|
346
|
+
updateConfig(newFiles) {
|
|
347
|
+
const newAudioFiles = Object.fromEntries(Object.entries(newFiles).filter((file) => {
|
|
348
|
+
const type = file[1].type;
|
|
349
|
+
// COGS 4.6 did not send a `type` but only reported audio files
|
|
350
|
+
// so we assume audio if no `type` is given
|
|
351
|
+
return type === 'audio' || !type;
|
|
352
|
+
}));
|
|
353
|
+
const previousClipPlayers = this.audioClipPlayers;
|
|
354
|
+
this.audioClipPlayers = (() => {
|
|
355
|
+
const clipPlayers = { ...previousClipPlayers };
|
|
356
|
+
const removedClips = Object.keys(previousClipPlayers).filter((previousFile) => !(previousFile in newAudioFiles) && !previousClipPlayers[previousFile].config.ephemeral);
|
|
357
|
+
removedClips.forEach((file) => {
|
|
358
|
+
const player = previousClipPlayers[file].player;
|
|
359
|
+
player.unload();
|
|
360
|
+
delete clipPlayers[file];
|
|
361
|
+
});
|
|
362
|
+
const addedClips = Object.entries(newAudioFiles).filter(([newfile]) => !previousClipPlayers[newfile]);
|
|
363
|
+
addedClips.forEach(([path, config]) => {
|
|
364
|
+
clipPlayers[path] = this.createClip(path, { ...config, ephemeral: false });
|
|
365
|
+
});
|
|
366
|
+
const updatedClips = Object.keys(previousClipPlayers).filter((previousFile) => previousFile in newAudioFiles);
|
|
367
|
+
updatedClips.forEach((path) => {
|
|
368
|
+
clipPlayers[path] = this.updatedClip(path, clipPlayers[path], { ...newAudioFiles[path], ephemeral: false });
|
|
369
|
+
});
|
|
370
|
+
return clipPlayers;
|
|
371
|
+
})();
|
|
372
|
+
this.notifyStateListeners();
|
|
373
|
+
}
|
|
374
|
+
notifyStateListeners() {
|
|
375
|
+
const clips = Object.entries(this.audioClipPlayers).reduce((clips, [path, clipPlayer]) => {
|
|
376
|
+
clips[path] = {
|
|
377
|
+
config: { preload: clipPlayer.config.preload, ephemeral: clipPlayer.config.ephemeral },
|
|
378
|
+
activeClips: clipPlayer.activeClips,
|
|
379
|
+
};
|
|
380
|
+
return clips;
|
|
381
|
+
}, {});
|
|
382
|
+
const isPlaying = Object.values(this.audioClipPlayers).some(({ activeClips }) => Object.values(activeClips).some((clip) => clip.state.type === 'playing' || clip.state.type === 'pausing' || clip.state.type === 'stopping'));
|
|
383
|
+
const audioState = {
|
|
384
|
+
globalVolume: this.globalVolume,
|
|
385
|
+
isPlaying,
|
|
386
|
+
clips,
|
|
387
|
+
};
|
|
388
|
+
this.dispatchEvent('state', audioState);
|
|
389
|
+
}
|
|
390
|
+
notifyClipStateListeners(playId, file, status) {
|
|
391
|
+
this.dispatchEvent('audioClipState', { mediaType: 'audio', playId, file, status });
|
|
392
|
+
}
|
|
393
|
+
// Type-safe wrapper around EventTarget
|
|
394
|
+
addEventListener(type, listener, options) {
|
|
395
|
+
this.eventTarget.addEventListener(type, listener, options);
|
|
396
|
+
}
|
|
397
|
+
removeEventListener(type, listener, options) {
|
|
398
|
+
this.eventTarget.removeEventListener(type, listener, options);
|
|
399
|
+
}
|
|
400
|
+
dispatchEvent(type, detail) {
|
|
401
|
+
this.eventTarget.dispatchEvent(new CustomEvent(type, { detail }));
|
|
402
|
+
}
|
|
403
|
+
createPlayer(path, config) {
|
|
404
|
+
const player = new Howl({
|
|
405
|
+
src: this.cogsConnection.getAssetUrl(path),
|
|
406
|
+
autoplay: false,
|
|
407
|
+
loop: false,
|
|
408
|
+
volume: 1,
|
|
409
|
+
html5: true,
|
|
410
|
+
preload: config.preload,
|
|
411
|
+
});
|
|
412
|
+
setPlayerSinkId(player, this.sinkId);
|
|
413
|
+
return player;
|
|
414
|
+
}
|
|
415
|
+
createClip(file, config) {
|
|
416
|
+
return {
|
|
417
|
+
config,
|
|
418
|
+
player: this.createPlayer(file, config),
|
|
419
|
+
activeClips: {},
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
updatedClip(clipPath, previousClip, newConfig) {
|
|
423
|
+
const clip = { ...previousClip, config: newConfig };
|
|
424
|
+
if (previousClip.config.preload !== newConfig.preload) {
|
|
425
|
+
clip.player.unload();
|
|
426
|
+
clip.player = this.createPlayer(clipPath, newConfig);
|
|
427
|
+
}
|
|
428
|
+
return clip;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
function log(...data) {
|
|
432
|
+
if (DEBUG) {
|
|
433
|
+
console.log(...data);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* @returns `true` if this is this a valid fade duration. Always returns `false` on iOS
|
|
438
|
+
*/
|
|
439
|
+
function isFadeValid(fade) {
|
|
440
|
+
return !IS_IOS && typeof fade === 'number' && !isNaN(fade) && fade > 0;
|
|
441
|
+
}
|
|
442
|
+
function setPlayerSinkId(player, sinkId) {
|
|
443
|
+
if (sinkId === undefined) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (player._html5) {
|
|
447
|
+
player._sounds?.forEach((sound) => {
|
|
448
|
+
sound._node?.setSinkId?.(sinkId);
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
// TODO: handle web audio
|
|
453
|
+
console.warn('Cannot set sink ID: web audio not supported', player);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Set audio volume
|
|
458
|
+
*
|
|
459
|
+
* This doesn't work on iOS (volume is read-only) so at least mute it if the volume is zero
|
|
460
|
+
*/
|
|
461
|
+
function setAudioPlayerVolume(howl, volume, soundId) {
|
|
462
|
+
log('Setting volume', volume, soundId);
|
|
463
|
+
howl.volume(volume, soundId);
|
|
464
|
+
howl.mute(volume === 0, soundId);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Fade to audio volume
|
|
468
|
+
*
|
|
469
|
+
* Note: This doesn't work on iOS (volume is read-only)
|
|
470
|
+
*/
|
|
471
|
+
function fadeAudioPlayerVolume(howl, volume, fade, soundId) {
|
|
472
|
+
howl.fade(howl.volume(soundId), volume, fade, soundId);
|
|
473
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { MediaObjectFit } from '.';
|
|
2
|
+
import CogsConnection from './CogsConnection';
|
|
3
|
+
import MediaClipStateMessage from './types/MediaClipStateMessage';
|
|
4
|
+
import { VideoState } from './types/VideoState';
|
|
5
|
+
type EventTypes = {
|
|
6
|
+
state: VideoState;
|
|
7
|
+
videoClipState: MediaClipStateMessage;
|
|
8
|
+
};
|
|
9
|
+
export default class VideoPlayer {
|
|
10
|
+
private cogsConnection;
|
|
11
|
+
private eventTarget;
|
|
12
|
+
private globalVolume;
|
|
13
|
+
private videoClipPlayers;
|
|
14
|
+
private activeClip?;
|
|
15
|
+
private pendingClip?;
|
|
16
|
+
private parentElement;
|
|
17
|
+
private sinkId;
|
|
18
|
+
constructor(cogsConnection: CogsConnection<any>, parentElement?: HTMLElement);
|
|
19
|
+
setParentElement(parentElement: HTMLElement): void;
|
|
20
|
+
resetParentElement(): void;
|
|
21
|
+
setGlobalVolume(globalVolume: number): void;
|
|
22
|
+
playVideoClip(path: string, { playId, volume, loop, fit }: {
|
|
23
|
+
playId: string;
|
|
24
|
+
volume: number;
|
|
25
|
+
loop: boolean;
|
|
26
|
+
fit: MediaObjectFit;
|
|
27
|
+
}): void;
|
|
28
|
+
pauseVideoClip(): void;
|
|
29
|
+
stopVideoClip(): void;
|
|
30
|
+
setVideoClipVolume({ volume }: {
|
|
31
|
+
volume: number;
|
|
32
|
+
}): void;
|
|
33
|
+
setVideoClipFit({ fit }: {
|
|
34
|
+
fit: MediaObjectFit;
|
|
35
|
+
}): void;
|
|
36
|
+
private handleStoppedClip;
|
|
37
|
+
private updateVideoClipPlayer;
|
|
38
|
+
setAudioSink(sinkId: string): void;
|
|
39
|
+
private updateConfig;
|
|
40
|
+
private notifyStateListeners;
|
|
41
|
+
private notifyClipStateListeners;
|
|
42
|
+
addEventListener<EventName extends keyof EventTypes>(type: EventName, listener: (ev: CustomEvent<EventTypes[EventName]>) => void, options?: boolean | AddEventListenerOptions): void;
|
|
43
|
+
removeEventListener<EventName extends keyof EventTypes>(type: EventName, listener: (ev: CustomEvent<EventTypes[EventName]>) => void, options?: boolean | EventListenerOptions): void;
|
|
44
|
+
private dispatchEvent;
|
|
45
|
+
private createVideoElement;
|
|
46
|
+
private createClipPlayer;
|
|
47
|
+
private unloadClip;
|
|
48
|
+
}
|
|
49
|
+
export {};
|