mempool/frontend/src/app/services/audio.service.ts

30 lines
691 B
TypeScript
Raw Normal View History

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