25 lines
730 B
JavaScript
25 lines
730 B
JavaScript
/**
|
||
* Воспроизводит фоновую музыку
|
||
* @param {Object} config объект с параметрами движка
|
||
* @param {AudioContext} music
|
||
*/
|
||
export function playMusic(config, music) {
|
||
let request = new XMLHttpRequest();
|
||
|
||
request.open("GET", `assets/sfx/${config.music}`, true);
|
||
request.responseType = "arraybuffer";
|
||
request.onload = function() {
|
||
music.decodeAudioData(request.response, onDecoded);
|
||
}
|
||
|
||
function onDecoded(buffer) {
|
||
let bufferSource = music.createBufferSource();
|
||
bufferSource.buffer = buffer;
|
||
bufferSource.connect(music.destination);
|
||
bufferSource.loop = true;
|
||
bufferSource.start();
|
||
}
|
||
|
||
request.send();
|
||
}
|