Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.2 KiB

/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
const ASSETS_PATH = 'assets/audio/';
export default class AudioAssetPlayer<AssetName extends string> {
private audio: HTMLAudioElement;
private tempId: number;
constructor(private assets: AssetName[]) {
this.tempId = 0;
}
public playSound(name: AssetName, loop = false) {
++this.tempId;
try {
const audio = this.createAudio();
audio.src = ASSETS_PATH + name;
audio.loop = loop;
audio.play();
} catch(e) {
console.error('playSound', name, e);
}
}
public createAudio() {
let {audio} = this;
if(audio) {
return audio;
}
audio = this.audio = new Audio();
audio.play();
return audio;
}
public stopSound() {
this.audio.pause();
}
public cancelDelayedPlay() {
++this.tempId;
}
public playSoundWithTimeout(name: AssetName, loop: boolean, timeout: number) {
// timeout = 0;
const tempId = ++this.tempId;
setTimeout(() => {
if(this.tempId !== tempId) {
return;
}
this.playSound(name, loop);
}, timeout);
}
}