1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

simple audio player

This commit is contained in:
Fedor Katurov 2019-10-11 13:51:14 +07:00
parent c11e281965
commit 5cad13b417
5 changed files with 70 additions and 9 deletions

View file

@ -1,10 +1,55 @@
import { Howl } from 'howler';
// import { Howl } from 'howler';
// import { store } from '~/redux/store';
Howl.prototype.setSrc = function setSrc(src) {
this.unload();
this._src = src;
this.load();
this.play();
};
// export const Player: HTMLAudioElement = new Audio();
//
// console.log(Player);
export const Player = new Howl({ src: [''] });
export class PlayerClass {
public constructor() {
this.element.addEventListener('timeupdate', () => {
const { duration: total, currentTime: current } = this.element;
const progress = (current / total) * 100;
this.element.dispatchEvent(
new CustomEvent('playprogress', {
detail: { current, total, progress },
})
);
});
}
public element: HTMLAudioElement = new Audio();
public duration: number = 0;
public set = (src: string): void => {
this.element.src = src;
};
public on = (type: string, callback) => {
this.element.addEventListener(type, callback);
};
public off = (type: string, callback) => {
this.element.removeEventListener(type, callback);
};
public load = () => {
this.element.load();
};
public play = () => {
this.element.play();
};
public getDuration = () => {
return this.element.currentTime;
};
}
const Player = new PlayerClass();
Player.element.addEventListener('playprogress', ({ detail }: CustomEvent) => console.log(detail));
export { Player };