Skip to content

Instantly share code, notes, and snippets.

@jefBinomed
Created November 30, 2018 15:52
Show Gist options
  • Save jefBinomed/50df3ba9d7f688403121027701e3a85f to your computer and use it in GitHub Desktop.
Save jefBinomed/50df3ba9d7f688403121027701e3a85f to your computer and use it in GitHub Desktop.
2018-countdown-player-audio-es6-0
'use strict'
import {
PLAYLIST
} from './playlist.js';
/**
* Class for playing music
*
* We create an invisible audio element and we play music on it
*/
export class AudioPlayer {
constructor() {
this.indexPlayList = 0;
this.currentIndex = 0;
this.audioElt = document.createElement('audio');
this.audioElt.style.display = 'none';
document.body.appendChild(this.audioElt);
window.addEventListener('beforeunload', this._unload.bind(this));
this._startPlayer();
}
_startPlayer() {
if (localStorage['devfestCountdown-LastSong']) {
this.indexPlayList = +localStorage['devfestCountdown-LastSong'];
if (this.indexPlayList >= PLAYLIST.length) {
this._nextSong();
} else {
this._playSound(`./assets/audio/${PLAYLIST[this.indexPlayList]}`);
this.audioElt.currentTime = +localStorage['devfestCountdown-currentTime'];
}
} else {
this._nextSong();
}
}
_unload() {
localStorage['devfestCountdown-LastSong'] = `${this.currentIndex}`;
localStorage['devfestCountdown-currentTime'] = `${this.audioElt.currentTime}`;
}
/**
* Play a song according to the url of song
*/
_playSound(url) {
this.audioElt.pause();
this.audioElt.src = url;
this.audioElt.play();
this.audioElt.onended = this._nextSong.bind(this);
}
/**
* Skip to the next song
*/
_nextSong() {
try {
this.currentIndex = this.indexPlayList;
this._playSound(`./assets/audio/${PLAYLIST[this.indexPlayList]}`);
this.indexPlayList = (this.indexPlayList + 1) % PLAYLIST.length;
} catch (err) {
console.error(err);
}
}
/**
* Update the sound volume of audio element
*/
manageSoundVolume(delta) {
if (delta < 10 * 1000) {
this.audioElt.volume = Math.min(Math.max(0, delta / (10 * 1000)), 0.5);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment