1
0
Fork 1
mirror of https://example.com synced 2024-11-22 12:06:39 +09:00

fix: sound effects stop music playback in iOS

This also improves the overall sound performance (including Android PWA)

Co-authored-by: naskya <m@naskya.net>
This commit is contained in:
osamu 2023-11-27 20:32:48 +09:00 committed by naskya
parent 9fcd3e496f
commit cefb6847ac
Signed by: naskya
GPG key ID: 164DFF24E2D40139
2 changed files with 28 additions and 11 deletions

View file

@ -38,6 +38,8 @@
## 細かい変更点
- iOS で効果音と音楽の再生が干渉する問題を修正Misskey から取り込み)
- 本家にもマージリクエストを出しています ([!10641](https://git.joinfirefish.org/firefish/firefish/-/merge_requests/10641))
- HTML のコードに入るコメントアートを削除
- 全ページにこんなの入れなくても……
- デフォルトではバイブレーションを無効に

View file

@ -1,16 +1,25 @@
import { ColdDeviceStorage } from "@/store";
const ctx = new AudioContext();
const cache = new Map<string, HTMLAudioElement>();
export function getAudio(file: string, useCache = true): HTMLAudioElement {
let audio: HTMLAudioElement;
export async function getAudio(
file: string,
useCache = true,
): HTMLAudioElement {
if (useCache && cache.has(file)) {
audio = cache.get(file);
} else {
audio = new Audio(`/static-assets/sounds/${file}.mp3`);
if (useCache) cache.set(file, audio);
return cache.get(file);
}
return audio;
const response = await fetch(`/static-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(
@ -28,11 +37,17 @@ export function play(type: string) {
playFile(sound.type, sound.volume);
}
export function playFile(file: string, volume: number) {
export async function playFile(file: string, volume: number) {
const masterVolume = ColdDeviceStorage.get("sound_masterVolume");
if (masterVolume === 0 || volume === 0 || file.toLowerCase().includes("none"))
if (masterVolume === 0 || volume === 0) {
return;
}
const audio = setVolume(getAudio(file), volume);
audio.play();
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();
}