quizEngine/js/music.js

25 lines
730 B
JavaScript
Raw Permalink Normal View History

2021-07-08 01:38:10 +03:00
/**
* Воспроизводит фоновую музыку
* @param {Object} config объект с параметрами движка
* @param {AudioContext} music
*/
2021-07-06 22:51:48 +03:00
export function playMusic(config, music) {
let request = new XMLHttpRequest();
2021-07-07 00:04:35 +03:00
request.open("GET", `assets/sfx/${config.music}`, true);
2021-07-06 22:51:48 +03:00
request.responseType = "arraybuffer";
2021-07-07 00:04:35 +03:00
request.onload = function() {
2021-07-06 22:51:48 +03:00
music.decodeAudioData(request.response, onDecoded);
}
function onDecoded(buffer) {
2021-07-07 00:04:35 +03:00
let bufferSource = music.createBufferSource();
2021-07-06 22:51:48 +03:00
bufferSource.buffer = buffer;
bufferSource.connect(music.destination);
bufferSource.loop = true;
2021-07-07 00:04:35 +03:00
bufferSource.start();
2021-07-06 22:51:48 +03:00
}
request.send();
}