AudioBufferSourceNode.start() 不起作用,或者至少激活但不产生任何声音,我做错了什么吗?
AudioBufferSourceNode.start() doesn't work, or at least activate but doesn t produce any sound, am i doing something wrong?
my goal is to play a sound when a specific key is pressed (me saying the letter on the key pressed), but without html audio tag, with audioBuffers from web-Audio API.
Execpt here, the audioBufferSourceNode created as an item of arraySources, doesn t display any sound when the function .start() is used on it. Dont know whyy
UPDATE : SOLVED arraySources[count].connect(AudCtx.destination) was missing .. !
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>
请看看这个答案,它应该能达到您想要达到的目的:
How to make a Web Audio element sound instantly when pressed on mobile?
my goal is to play a sound when a specific key is pressed (me saying the letter on the key pressed), but without html audio tag, with audioBuffers from web-Audio API.
Execpt here, the audioBufferSourceNode created as an item of arraySources, doesn t display any sound when the function .start() is used on it. Dont know whyyUPDATE : SOLVED arraySources[count].connect(AudCtx.destination) was missing .. !
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>
请看看这个答案,它应该能达到您想要达到的目的:
How to make a Web Audio element sound instantly when pressed on mobile?