This repository has been archived on 2025-06-18. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
casino/frontend/src/app/shared/services/audio.service.ts
Jan-Marlon Leibl 566ea569e1
All checks were successful
CI / Get Changed Files (pull_request) Successful in 8s
CI / Checkstyle Main (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Successful in 18s
CI / eslint (pull_request) Successful in 26s
CI / prettier (pull_request) Successful in 25s
CI / Docker frontend validation (pull_request) Successful in 42s
CI / test-build (pull_request) Successful in 44s
refactor(audio.service): improve audio caching logic
2025-05-15 14:14:18 +02:00

30 lines
822 B
TypeScript

import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class AudioService {
private audioCache = new Map<string, HTMLAudioElement>();
private getAudio(soundName: string): HTMLAudioElement {
if (this.audioCache.has(soundName)) {
return this.audioCache.get(soundName)!;
}
const audio = new Audio(`/sounds/${soundName}.mp3`);
this.audioCache.set(soundName, audio);
return audio;
}
playBetSound(): void {
const audio = this.getAudio('bet.mp3');
audio.currentTime = 0;
audio.play().catch((error) => console.error('Error playing bet sound:', error));
}
playWinSound(): void {
const audio = this.getAudio('win.mp3');
audio.currentTime = 0;
audio.play().catch((error) => console.error('Error playing win sound:', error));
}
}