【问题标题】:AudioBufferSourceNode.start() doesn't work, or at least activate but doesn t produce any sound, am i doing something wrong?AudioBufferSourceNode.start() 不起作用,或者至少激活但不产生任何声音,我做错了什么吗?
【发布时间】:2020-10-05 20:13:01
【问题描述】:

我的目标是在按下特定键时播放声音(我说按下键上的字母),但没有 html 音频标签,使用来自 web-Audio API 的 audioBuffers。
此处执行,作为arraySources的一项创建的audioBufferSourceNode在使用.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>

【问题讨论】:

    标签: javascript audio buffer web-audio-api audiobuffer


    【解决方案1】:

    请看一下这个答案,它应该能达到你想要达到的效果:

    How to make a Web Audio element sound instantly when pressed on mobile?

    【讨论】:

    • 谢谢,这让我意识到我错过了 .connect 函数,我的缓冲区源没有目的地...
    猜你喜欢
    • 2021-04-17
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多