feat: added use audio
This commit is contained in:
16
client/src/composables/libAudio.ts
Normal file
16
client/src/composables/libAudio.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
/** Получает путь к аудио по id */
|
||||||
|
export function getSoundPathById(id: string): string {
|
||||||
|
const sound = SOUND_LIBRARY.find(s => s.id === id);
|
||||||
|
|
||||||
|
if (!sound) {
|
||||||
|
throw new Error("Sound not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
return sound.file;
|
||||||
|
}
|
||||||
|
|
||||||
|
const base_path = "/sounds"
|
||||||
|
|
||||||
|
const SOUND_LIBRARY = [
|
||||||
|
{ id: "alarm_beep", file: `${base_path}/alarm-beep.mp3`, name: "Alarm Beep" }
|
||||||
|
];
|
||||||
49
client/src/composables/useAudio.ts
Normal file
49
client/src/composables/useAudio.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
let audioContext: AudioContext | null = null;
|
||||||
|
let currentSource: AudioBufferSourceNode | null = null;
|
||||||
|
|
||||||
|
/** Получить AudioContext */
|
||||||
|
function getAudioContext(): AudioContext {
|
||||||
|
if (!audioContext) {
|
||||||
|
audioContext = new AudioContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
return audioContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Воспроизводит звук */
|
||||||
|
export async function playSound(url: string): Promise<void> {
|
||||||
|
const context = getAudioContext();
|
||||||
|
|
||||||
|
if (context.state === 'suspended') {
|
||||||
|
await context.resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Если уже играет звук — остановит
|
||||||
|
stopSound();
|
||||||
|
|
||||||
|
const response = await fetch(url);
|
||||||
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
|
const audioBuffer = await context.decodeAudioData(arrayBuffer);
|
||||||
|
const source = context.createBufferSource();
|
||||||
|
|
||||||
|
source.buffer = audioBuffer;
|
||||||
|
source.connect(context.destination);
|
||||||
|
currentSource = source;
|
||||||
|
source.start(0);
|
||||||
|
source.onended = () => {
|
||||||
|
source.disconnect();
|
||||||
|
|
||||||
|
if (currentSource === source) {
|
||||||
|
currentSource = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Останавливает звук */
|
||||||
|
export function stopSound(): void {
|
||||||
|
if (currentSource) {
|
||||||
|
currentSource.stop();
|
||||||
|
currentSource.disconnect();
|
||||||
|
currentSource = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user