quizEngine/js/music.js

20 lines
555 B
JavaScript
Raw Normal View History

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();
}