class AudioPlayer {
  audio: HTMLAudioElement;

  constructor() {
    this.audio = new Audio();
    this.audio.preload = 'none';
  }

  async play(src: string) {
    try {
      if (!src) return;
      this.audio.pause();
      this.audio.src = src;
      this.audio.currentTime = 0;
      await this.audio.play();
    } catch (err) {
      console.error('Play error:', err);
    }
  }

  pause() {
    this.audio.pause();
  }

  async resume() {
    try {
      await this.audio.play();
    } catch (err) {
      console.error(err);
    }
  }

  seek(time: number) {
    this.audio.currentTime = time;
  }

  setVolume(volume: number) {
    this.audio.volume = volume / 100;
  }
}

export const audioPlayer = new AudioPlayer();
