AudioBufferSourceNode.start () не работает или, по крайней мере, не активируется, но не производит никакого звука, я что-то делаю не так? - PullRequest
1 голос
/ 16 июня 2020

моя цель - воспроизвести звук при нажатии определенной клавиши c (я говорю букву на нажатой клавише), но без тега html audio, с audioBuffers из web-Audio API. За исключением здесь, audioBufferSourceNode, созданный как элемент arraySources, не отображает никакого звука, когда на нем используется функция .start (). Не знаю, почему

ОБНОВЛЕНИЕ: РЕШЕНИЕ arraySources [count] .connect (AudCtx.destination) отсутствует ..!

var myHeader = new Headers({
    'accept':'audio/mpeg',
    'cache-controle':'private',
});

var myInit = {  method: 'GET',
                headers: myHeader,
                mode: 'cors',
                cache:'default' };

var AudCtx = new AudioContext();
arrayRes=[];
arrayBuffers=[];
arraySources=[];

for (var i=0;i<26;i++){
    fetch('./audio/abc'+(i+1)+'.mp3',myInit).then(function(response){
        arrayRes.push(response);
        console.log(response);
        return response;
    })
    .then(function(response){
        return response.arrayBuffer();
    })
    .then(function(buffer){
        console.log(buffer);
        return AudCtx.decodeAudioData(buffer);
    })
    .then(function(decodedData){
        arrayBuffers.push(decodedData);
        console.log(decodedData);
    })
}

var count = 0; 
window.addEventListener("keydown",function(e){
    var keycode = e.keyCode;
    var posiArray = e.keyCode-65;
    if (keycode>=65 && keycode<=91){
        console.log("keycode = "+keycode+" & posiArray = "+posiArray);
        console.log("count = "+count);
        arraySources[count] = AudCtx.createBufferSource();
        arraySources[count].buffer = arrayBuffers[posiArray];
        count++;
    }
})

window.addEventListener("click",function(){
    arraySources[count-1].start(0);
})
<!DOCTYPE html>
<html>
    <head>
        <title>web audio test</title>
        <meta charset="UTF-8">
    </head>
    <body>

        <script src="script.js"></script>
    </body>
</html>

1 Ответ

0 голосов
/ 16 июня 2020

Пожалуйста, взгляните на этот ответ, он должен делать то, что вы пытаетесь достичь:

Как сделать так, чтобы элемент веб-аудио звучал мгновенно при нажатии на мобильном телефоне?

...