{"size": 743, "ext": "js", "max_stars_repo_path": "website/static/isaac/js/minesweeper/js/MusicPlayer.js", "max_stars_repo_name": "contactandyc/standard-template-library-alternative-in-c", "max_stars_repo_stars_event_min_datetime": "2019-09-24T19:05:42.000Z", "max_stars_repo_stars_event_max_datetime": "2019-09-24T19:52:55.000Z", "max_issues_repo_path": "website/static/isaac/js/minesweeper/js/MusicPlayer.js", "max_issues_repo_name": "contactandyc/standard-template-library-alternative-in-c", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "website/static/isaac/js/minesweeper/js/MusicPlayer.js", "max_forks_repo_name": "contactandyc/standard-template-library-alternative-in-c", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.2285714286, "max_line_length": 64, "alphanum_fraction": 0.5639300135}
export default class MusicPlayer extends Audio {
  constructor() {
    super();
    this.playlist = [];
    this.currentTrack = 0;
    this.addEventListener('ended', () => {
      this.startNextTrack();
    });
  }

  startNextTrack() {
    if (this.playlist.length > 0) {
      let numTracks = this.playlist.length;
      this.currentTrack++;
      if (this.currentTrack >= numTracks) this.currentTrack = 0;
      this.src = this.playlist[this.currentTrack];
      this.load();
      this.play();
    }
  }

  play() {
    if (this.src === '')
      if (this.playlist[0] != undefined) {
        this.src = this.playlist[0];
        this.load();
      } else return;
    super.play();
  }

  addTrack(src) {
    this.playlist.push(src);
  }
}
