Skip to content

Commit 081ba5e

Browse files
committed
chat notify sound
1 parent 733aa87 commit 081ba5e

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ verseModule
5252
.initialize(enableXrController)
5353
.initialize(enableMoveController)
5454
.initialize(nameLog, {})
55-
.initialize(chat, { balloon: true });
55+
.initialize(chat, {
56+
balloon: true,
57+
notifySound: { audioSrc: "./asset/ui/chat.m4a" },
58+
});
5659
```
5760

5861
see [examples/src/main.ts](https://github.com/Narazaka/verseengine-modules/blob/master/examples/src/main.ts)

examples/public/asset/ui/chat.m4a

3.13 KB
Binary file not shown.

examples/src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ function main() {
6262
.initialize(enableXrController)
6363
.initialize(enableMoveController)
6464
.initialize(nameLog, {})
65-
.initialize(chat, { balloon: true });
65+
.initialize(chat, {
66+
balloon: true,
67+
notifySound: { audioSrc: "./asset/ui/chat.m4a" },
68+
});
6669
});
6770

6871
if ("xr" in navigator && navigator.xr) {

src/chat.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getOrAddSprite } from "./util/graphic/getOrAddSprite";
1212
import { getOrAddNameplateContainer } from "./util/graphic/getOrAddNameplateContainer";
1313
import { createTextSpriteData } from "./util/graphic/createTextSpriteData";
1414
import { drawRoundedRectPath } from "./util/graphic/drawRoundedRectPath";
15+
import { createCachedAudio } from "./util/cachedAudio";
1516

1617
const chatMessageMaxLength = 200;
1718

@@ -129,6 +130,8 @@ function handleChatBalloon(
129130
balloon.scale.set(scale.x, scale.y, scale.z);
130131
}
131132

133+
const { getAudio, createAudio } = createCachedAudio();
134+
132135
export type ChatMessageData = {
133136
chatMessage?: string;
134137
};
@@ -148,6 +151,10 @@ export default ({
148151
onChatMessage: (chatMessage: string) => unknown,
149152
) => unknown;
150153
addLog?: ((message: string) => unknown) | false;
154+
notifySound?: {
155+
audioSrc: string;
156+
volume?: number;
157+
};
151158
balloon?:
152159
| true
153160
| {
@@ -173,12 +180,23 @@ export default ({
173180
: options?.addLog || getDefaultAddLog(domRoot);
174181
const balloonOption = options?.balloon === true ? {} : options?.balloon;
175182

183+
if (options?.notifySound) {
184+
createAudio(
185+
domRoot,
186+
options.notifySound.audioSrc,
187+
options.notifySound.volume,
188+
);
189+
}
190+
176191
addTextDataChangedListener((otherPerson, data) => {
177192
if (data.chatMessage) {
178193
if (addLog) {
179194
const message = makeLogMessage(data);
180195
addLog(message!);
181196
}
197+
if (options?.notifySound) {
198+
getAudio()?.play();
199+
}
182200
}
183201
if (balloonOption) {
184202
handleChatBalloon(

src/joinSound.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,18 @@
44
*/
55
import type { VerseModuleBase } from "./VerseModuleBase";
66
import { PlayerSessionIdData } from "./playerSessionId";
7-
8-
let $audioCache: HTMLAudioElement | undefined;
7+
import { createCachedAudio } from "./util/cachedAudio";
98

109
/** @internal */
11-
export function createJoinAudio(
12-
parent: HTMLElement,
13-
audioSrc: string,
14-
volume = 0.03,
15-
) {
16-
if ($audioCache) return $audioCache;
17-
const $audio = document.createElement("audio");
18-
$audio.preload = "auto";
19-
$audio.src = audioSrc;
20-
$audio.volume = volume;
21-
parent.appendChild($audio);
22-
$audioCache = $audio;
23-
return $audio;
24-
}
10+
const { getAudio, createAudio } = createCachedAudio();
2511

2612
const ids = new Set<string>();
2713

2814
/** @internal */
2915
export function handleJoinSound(id: string) {
3016
if (ids.has(id)) return;
3117
ids.add(id);
32-
$audioCache?.play();
18+
getAudio()?.play();
3319
}
3420

3521
/**
@@ -40,7 +26,7 @@ export default ({
4026
domRoot,
4127
}: VerseModuleBase<{}, PlayerSessionIdData>) => ({
4228
initialize(options: { audioSrc: string; volume?: number }) {
43-
createJoinAudio(domRoot, options.audioSrc, options.volume);
29+
createAudio(domRoot, options.audioSrc, options.volume);
4430
addTextDataChangedListener((_, data) => {
4531
handleJoinSound(data.playerSessionId);
4632
});

src/util/cachedAudio.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function createCachedAudio() {
2+
let $audioCache: HTMLAudioElement | undefined;
3+
4+
return {
5+
createAudio(parent: HTMLElement, audioSrc: string, volume = 0.03) {
6+
if ($audioCache) return $audioCache;
7+
const $audio = document.createElement("audio");
8+
$audio.preload = "auto";
9+
$audio.src = audioSrc;
10+
$audio.volume = volume;
11+
parent.appendChild($audio);
12+
$audioCache = $audio;
13+
return $audio;
14+
},
15+
getAudio() {
16+
return $audioCache;
17+
},
18+
};
19+
}

0 commit comments

Comments
 (0)