2020-02-25 22:29:57 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AudioService {
|
2020-11-06 22:30:52 +01:00
|
|
|
audio: HTMLAudioElement;
|
2020-06-10 18:52:14 +02:00
|
|
|
isPlaying = false;
|
2020-02-25 22:29:57 +01:00
|
|
|
|
2020-11-06 22:30:52 +01:00
|
|
|
constructor() {
|
|
|
|
try {
|
|
|
|
this.audio = new Audio();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
2020-02-25 22:29:57 +01:00
|
|
|
|
2020-06-10 18:52:14 +02:00
|
|
|
public playSound(name: 'magic' | 'chime' | 'cha-ching' | 'bright-harmony') {
|
2020-11-06 22:30:52 +01:00
|
|
|
if (this.isPlaying || !this.audio) {
|
2020-06-10 18:52:14 +02:00
|
|
|
return;
|
2020-02-25 22:29:57 +01:00
|
|
|
}
|
2020-06-10 18:52:14 +02:00
|
|
|
this.isPlaying = true;
|
|
|
|
this.audio.src = '../../../resources/sounds/' + name + '.mp3';
|
|
|
|
this.audio.load();
|
2021-03-12 07:20:40 +01:00
|
|
|
this.audio.volume = 0.65; // 65% volume
|
2020-06-10 18:52:14 +02:00
|
|
|
this.audio.play().catch((e) => {
|
2020-10-13 10:27:52 +02:00
|
|
|
console.log('Play sound failed' + e);
|
2020-06-10 18:52:14 +02:00
|
|
|
});
|
|
|
|
setTimeout(() => this.isPlaying = false, 100);
|
2020-02-25 22:29:57 +01:00
|
|
|
}
|
|
|
|
}
|