From 38d6580a36bb6474153536edb50b59376ce16779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8A=E3=81=95=E3=82=80=E3=81=AE=E3=81=B2=E3=81=A8?= <46447427+samunohito@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:03:15 +0900 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E9=9F=B3=E3=81=AA=E3=81=A9?= =?UTF-8?q?=E3=81=AE=E7=99=BA=E9=9F=B3=E6=96=B9=E6=B3=95=E3=82=92=E5=A4=89?= =?UTF-8?q?=E3=81=88=E3=80=81iOS=E3=81=A7=E9=9F=B3=E5=A3=B0=E5=87=BA?= =?UTF-8?q?=E5=8A=9B=E3=81=8C=E7=AB=B6=E5=90=88=E3=81=97=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B=20(#12339)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * HTMLAudioElementを使わないようにする * fix CHANGELOG.md --------- Co-authored-by: osamu <46447427+sam-osamu@users.noreply.github.com> --- CHANGELOG.md | 1 + packages/frontend/src/scripts/sound.ts | 39 ++++++++++++++++++-------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d872ff153b..6efb78b145 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - Fix: 非ログイン時に「メモを追加」を表示しないように変更 #12309 - Fix: 絵文字ピッカーでの検索が更新されない問題を修正 - Fix: 特定の条件下でノートがnyaizeされない問題を修正 +- Fix: iOSでの音声出力不備を改善 #12339 ### Server - Fix: トークンのないプラグインをアンインストールするときにエラーが出ないように 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(); +const ctx = new AudioContext(); +const cache = new Map(); 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(); }