|
1 | 1 | import { AudioPlayer, AudioPlayerOptions } from './audio-player'; |
2 | 2 |
|
3 | 3 | export type AudioPlayerPoolOptions = { |
4 | | - multipleAudioPlayers: boolean; |
| 4 | + allowConcurrentAudioPlayback: boolean; |
5 | 5 | }; |
6 | 6 |
|
7 | 7 | export class AudioPlayerPool { |
8 | | - audioPlayers: Map<string, AudioPlayer>; |
9 | | - multipleAudioPlayers: boolean; |
10 | | - constructor({ multipleAudioPlayers }: AudioPlayerPoolOptions) { |
11 | | - this.audioPlayers = new Map<string, AudioPlayer>(); |
12 | | - this.multipleAudioPlayers = multipleAudioPlayers ?? false; |
| 8 | + pool: Map<string, AudioPlayer>; |
| 9 | + allowConcurrentAudioPlayback: boolean; |
| 10 | + private currentlyPlayingId: string | null = null; |
| 11 | + |
| 12 | + constructor({ allowConcurrentAudioPlayback }: AudioPlayerPoolOptions) { |
| 13 | + this.pool = new Map<string, AudioPlayer>(); |
| 14 | + this.allowConcurrentAudioPlayback = allowConcurrentAudioPlayback ?? false; |
13 | 15 | } |
14 | 16 |
|
15 | | - getPlayers() { |
16 | | - return Array.from(this.audioPlayers.values()); |
| 17 | + get players() { |
| 18 | + return Array.from(this.pool.values()); |
17 | 19 | } |
18 | 20 |
|
19 | 21 | getOrAddPlayer(params: AudioPlayerOptions) { |
20 | | - const player = this.audioPlayers.get(params.id); |
| 22 | + const player = this.pool.get(params.id); |
21 | 23 | if (player) { |
22 | 24 | return player; |
23 | 25 | } |
24 | 26 | const newPlayer = new AudioPlayer(params); |
| 27 | + newPlayer.pool = this; |
25 | 28 |
|
26 | | - this.audioPlayers.set(params.id, newPlayer); |
| 29 | + this.pool.set(params.id, newPlayer); |
27 | 30 | return newPlayer; |
28 | 31 | } |
29 | 32 |
|
30 | 33 | removePlayer(id: string) { |
31 | | - const player = this.audioPlayers.get(id); |
| 34 | + const player = this.pool.get(id); |
32 | 35 | if (!player) return; |
33 | 36 | player.onRemove(); |
34 | | - this.audioPlayers.delete(id); |
| 37 | + this.pool.delete(id); |
| 38 | + |
| 39 | + // Clear tracking if this was the currently playing player |
| 40 | + if (!this.allowConcurrentAudioPlayback && this.currentlyPlayingId === id) { |
| 41 | + this.currentlyPlayingId = null; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + deregister(id: string) { |
| 46 | + if (this.pool.has(id)) { |
| 47 | + this.pool.delete(id); |
| 48 | + } |
35 | 49 | } |
36 | 50 |
|
37 | 51 | clear() { |
38 | | - for (const player of this.audioPlayers.values()) { |
| 52 | + for (const player of this.pool.values()) { |
39 | 53 | this.removePlayer(player.id); |
40 | 54 | } |
| 55 | + this.currentlyPlayingId = null; |
41 | 56 | } |
42 | 57 |
|
43 | | - play(id: string) { |
44 | | - const targetPlayer = this.audioPlayers.get(id); |
45 | | - if (!targetPlayer) return; |
| 58 | + requestPlay(id: string) { |
| 59 | + if (this.allowConcurrentAudioPlayback) return; |
46 | 60 |
|
47 | | - if (!this.multipleAudioPlayers) { |
48 | | - for (const [playerId, player] of this.audioPlayers) { |
49 | | - if (playerId !== id && player.isPlaying) { |
50 | | - // eslint-disable-next-line no-underscore-dangle |
51 | | - player._pauseInternal(); |
52 | | - } |
| 61 | + if (this.currentlyPlayingId && this.currentlyPlayingId !== id) { |
| 62 | + const currentPlayer = this.pool.get(this.currentlyPlayingId); |
| 63 | + if (currentPlayer && currentPlayer.isPlaying) { |
| 64 | + currentPlayer.pause(); |
53 | 65 | } |
54 | 66 | } |
55 | | - // eslint-disable-next-line no-underscore-dangle |
56 | | - targetPlayer._playInternal(); |
57 | | - } |
58 | | - |
59 | | - pause(id: string) { |
60 | | - const targetPlayer = this.audioPlayers.get(id); |
61 | | - if (!targetPlayer) return; |
62 | | - // eslint-disable-next-line no-underscore-dangle |
63 | | - targetPlayer._pauseInternal(); |
| 67 | + this.currentlyPlayingId = id; |
64 | 68 | } |
65 | 69 |
|
66 | | - toggle(id: string) { |
67 | | - const targetPlayer = this.audioPlayers.get(id); |
68 | | - if (!targetPlayer) return; |
69 | | - if (targetPlayer.isPlaying) { |
70 | | - this.pause(id); |
71 | | - } else { |
72 | | - this.play(id); |
| 70 | + notifyPaused(id: string) { |
| 71 | + if (this.currentlyPlayingId === id) { |
| 72 | + this.currentlyPlayingId = null; |
73 | 73 | } |
74 | 74 | } |
75 | 75 | } |
0 commit comments