diff options
| author | おさむのひと <46447427+samunohito@users.noreply.github.com> | 2023-11-15 18:03:15 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-15 18:03:15 +0900 |
| commit | 38d6580a36bb6474153536edb50b59376ce16779 (patch) | |
| tree | 21988002a61fcfc9f0b523354e83250d33dffeec /packages/frontend/src/scripts | |
| parent | fix(backend): 追加情報のカスタム絵文字がユーザー情報のtag... (diff) | |
| download | sharkey-38d6580a36bb6474153536edb50b59376ce16779.tar.gz sharkey-38d6580a36bb6474153536edb50b59376ce16779.tar.bz2 sharkey-38d6580a36bb6474153536edb50b59376ce16779.zip | |
通知音などの発音方法を変え、iOSで音声出力が競合しないようにする (#12339)
* HTMLAudioElementを使わないようにする
* fix CHANGELOG.md
---------
Co-authored-by: osamu <46447427+sam-osamu@users.noreply.github.com>
Diffstat (limited to 'packages/frontend/src/scripts')
| -rw-r--r-- | packages/frontend/src/scripts/sound.ts | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/packages/frontend/src/scripts/sound.ts b/packages/frontend/src/scripts/sound.ts index f995c122d1..4b0cd0bb39 100644 --- a/packages/frontend/src/scripts/sound.ts +++ b/packages/frontend/src/scripts/sound.ts @@ -5,7 +5,8 @@ import { defaultStore } from '@/store.js'; -const cache = new Map<string, HTMLAudioElement>(); +const ctx = new AudioContext(); +const cache = new Map<string, AudioBuffer>(); export const soundsTypes = [ null, @@ -60,15 +61,20 @@ export const soundsTypes = [ 'noizenecio/kick_gaba7', ] as const; -export function getAudio(file: string, useCache = true): HTMLAudioElement { - let audio: HTMLAudioElement; +export async function getAudio(file: string, useCache = true) { if (useCache && cache.has(file)) { - audio = cache.get(file); - } else { - audio = new Audio(`/client-assets/sounds/${file}.mp3`); - if (useCache) cache.set(file, audio); + return cache.get(file)!; } - return audio; + + const response = await fetch(`/client-assets/sounds/${file}.mp3`); + const arrayBuffer = await response.arrayBuffer(); + const audioBuffer = await ctx.decodeAudioData(arrayBuffer); + + if (useCache) { + cache.set(file, audioBuffer); + } + + return audioBuffer; } export function setVolume(audio: HTMLAudioElement, volume: number): HTMLAudioElement { @@ -84,8 +90,17 @@ export function play(type: 'noteMy' | 'note' | 'antenna' | 'channel' | 'notifica playFile(sound.type, sound.volume); } -export function playFile(file: string, volume: number) { - const audio = setVolume(getAudio(file), volume); - if (audio.volume === 0) return; - audio.play(); +export async function playFile(file: string, volume: number) { + const masterVolume = defaultStore.state.sound_masterVolume; + if (masterVolume === 0 || volume === 0) { + return; + } + + const gainNode = ctx.createGain(); + gainNode.gain.value = masterVolume * volume; + + const soundSource = ctx.createBufferSource(); + soundSource.buffer = await getAudio(file); + soundSource.connect(gainNode).connect(ctx.destination); + soundSource.start(); } |