All files / src music.js

0% Statements 0/44
0% Branches 0/1
0% Functions 0/1
0% Lines 0/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
/**
 * Module for dealing with music and sound
 */
export const Music = {

  /**
    * use this to play a sound/music
    * track_id must be in the form '#track_id'
    * track ids can be found in index.html
    * the "loop" parameter can be set to true if you want the sound to keep looping
    *
    * use the volume parameter to change the volume, from 0.001 to 1.0!
    *
    * if you want to add an audiofile, add it to assets/music and make a line
    * for it in index.html like the following:
    * <audio src="assets/music/yourfile.extension" id="yourID"></audio>
    * */
  playTrack(trackId, loop) {
    const song = document.querySelector(trackId);
    song.loop = !!loop;
    // plays the selected track
    return song.play();
  },
  /**
    * this function can be used to stop any sound or music track
    */
  stopTrack(trackId) {
    const song = document.querySelector(trackId);
    return song.pause();
  },

  /**
   * updateVolume.
   * Updates the global volume based on the 'rangeSlider' UI element
   * @returns {undefined}
   */
  updateVolume() {
    const newVolume = document.getElementById('rangeSlider').value;
    document.querySelectorAll('audio').forEach((element) => {
      element.volume = newVolume; // eslint-disable-line no-param-reassign
    });
    document.getElementById('range_value').innerText = `${parseInt((`${newVolume * 100}`), 10)}%`;
  },
};