fix(frontend): ドライブの音声が再生できない場合の処理を追加 (#14073)
* fix(frontend): ドライブの音声が再生できない場合の処理を追加 * Update Changelog * fix lint * Update packages/frontend/src/scripts/sound.ts * lint * Update sound.ts * fix merge mistakes * use shorthand operator --------- Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
This commit is contained in:
@@ -124,23 +124,33 @@ export async function loadAudio(url: string, options?: { useCache?: boolean; })
|
||||
*/
|
||||
export function playMisskeySfx(operationType: OperationType) {
|
||||
const sound = defaultStore.state[`sound_${operationType}`];
|
||||
playMisskeySfxFile(sound);
|
||||
playMisskeySfxFile(sound).then((succeed) => {
|
||||
if (!succeed && sound.type === '_driveFile_') {
|
||||
// ドライブファイルが存在しない場合はデフォルトのサウンドを再生する
|
||||
const soundName = defaultStore.def[`sound_${operationType}`].default.type as Exclude<SoundType, '_driveFile_'>;
|
||||
if (_DEV_) console.log(`Failed to play sound: ${sound.fileUrl}, so play default sound: ${soundName}`);
|
||||
playMisskeySfxFileInternal({
|
||||
type: soundName,
|
||||
volume: sound.volume,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* サウンド設定形式で指定された音声を再生する
|
||||
* @param soundStore サウンド設定
|
||||
*/
|
||||
export function playMisskeySfxFile(soundStore: SoundStore) {
|
||||
export async function playMisskeySfxFile(soundStore: SoundStore): Promise<boolean> {
|
||||
// 連続して再生しない
|
||||
if (!canPlay) return;
|
||||
if (!canPlay) return false;
|
||||
// ユーザーアクティベーションが必要な場合はそれがない場合は再生しない
|
||||
if ('userActivation' in navigator && !navigator.userActivation.hasBeenActive) return;
|
||||
if ('userActivation' in navigator && !navigator.userActivation.hasBeenActive) return false;
|
||||
// サウンドがない場合は再生しない
|
||||
if (soundStore.type === null || soundStore.type === '_driveFile_' && !soundStore.fileUrl) return;
|
||||
if (soundStore.type === null || soundStore.type === '_driveFile_' && !soundStore.fileUrl) return false;
|
||||
|
||||
canPlay = false;
|
||||
playMisskeySfxFileInternal(soundStore).finally(() => {
|
||||
return await playMisskeySfxFileInternal(soundStore).finally(() => {
|
||||
// ごく短時間に音が重複しないように
|
||||
setTimeout(() => {
|
||||
canPlay = true;
|
||||
@@ -148,19 +158,22 @@ export function playMisskeySfxFile(soundStore: SoundStore) {
|
||||
});
|
||||
}
|
||||
|
||||
async function playMisskeySfxFileInternal(soundStore: SoundStore) {
|
||||
async function playMisskeySfxFileInternal(soundStore: SoundStore): Promise<boolean> {
|
||||
if (soundStore.type === null || (soundStore.type === '_driveFile_' && !soundStore.fileUrl)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
const masterVolume = defaultStore.state.sound_masterVolume;
|
||||
if (isMute() || masterVolume === 0 || soundStore.volume === 0) {
|
||||
return;
|
||||
return true; // ミュート時は成功として扱う
|
||||
}
|
||||
const url = soundStore.type === '_driveFile_' ? soundStore.fileUrl : `/client-assets/sounds/${soundStore.type}.mp3`;
|
||||
const buffer = await loadAudio(url);
|
||||
if (!buffer) return;
|
||||
const buffer = await loadAudio(url).catch(() => {
|
||||
return undefined;
|
||||
});
|
||||
if (!buffer) return false;
|
||||
const volume = soundStore.volume * masterVolume;
|
||||
createSourceNode(buffer, { volume }).soundSource.start();
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function playUrl(url: string, opts: {
|
||||
|
Reference in New Issue
Block a user