Javascript 网络音频:使事件发生在准确的时刻?
我试着做一些事情,声音样本是随机选择的间隔,使歌曲的发展,是不同的每次听。HTML音频不足以实现这一点,因为时间不精确,所以我正在试验Web音频,但它似乎相当复杂。现在,我只想知道如何让一个新的音频文件以16秒或32秒的准确时间播放。我遇到了这样的事情Javascript 网络音频:使事件发生在准确的时刻?,javascript,web-audio-api,Javascript,Web Audio Api,我试着做一些事情,声音样本是随机选择的间隔,使歌曲的发展,是不同的每次听。HTML音频不足以实现这一点,因为时间不精确,所以我正在试验Web音频,但它似乎相当复杂。现在,我只想知道如何让一个新的音频文件以16秒或32秒的准确时间播放。我遇到了这样的事情 playSound.start(audioContext.currentTime + numb); 但到目前为止,我无法让它工作 var audioContext=new audioContextCheck(); 函数audioFileLoad
playSound.start(audioContext.currentTime + numb);
但到目前为止,我无法让它工作
var audioContext=new audioContextCheck();
函数audioFileLoader(文件目录){
var soundObj={};
soundObj.fileDirectory=fileDirectory;
var getSound=newXMLHttpRequest();
打开(“GET”,soundObj.fileDirectory,true);
getSound.responseType=“arraybuffer”;
getSound.onload=函数(){
audioContext.decodeAudioData(getSound.response,函数(缓冲区){
soundObj.soundToPlay=缓冲区;
});
}
getSound.send();
soundObj.play=功能(音量、音高){
var volume=audioContext.createGain();
volume.gain.value=volumeVal;
var playSound=audioContext.createBufferSource();
playSound.playbackRate.value=pitchVal;
playSound.buffer=soundObj.soundToPlay;
播放声音。连接(音量);
volume.connect(audioContext.destination)
playSound.start(audioContext.currentTime)
}
返回soundObj;
};
var harp1=audioFileLoader(“爱尔兰/harp1.ogg”);
var harp2=audioFileLoader(“爱尔兰/harp2.ogg”);
功能键按下(){
竖琴1.演奏(.5,2);
harp2.启动(audioContext.currentTime+7.5);
}
window.addEventListener(“向下键”,按下键,错误);
你看,我正试图让harp2.ogg在harp1.ogg结束后立即播放。最终我希望能够随机选择下一个文件,但现在我只需要知道如何让它发生。如何使harp2.ogg在harp1.ogg开始后7.5秒完全播放(或者更好,有没有办法在harp2结束时触发它(音频中没有间隙)?)感谢帮助 WebAudio应该能够使用
start(time)
非常精确地启动音频,精确到最近的采样时间。如果没有,那是因为decodeAudioData
中的音频数据不包含您期望的数据,或者这是您的浏览器中的一个错误。看起来,当您按键时,您希望触发两首歌曲开始播放。一个马上,另一个在7.5秒内
播放歌曲的函数是soundObj.play
,它需要额外的参数,即播放歌曲的音频上下文时间。类似于:soundObj.play=function(volumeVal、pitchVal、startTime){…}
function keyPressed()
块的外观应如下所示:
harp1.play(.5,2,0);
harp2.start(1,1,audioContext.currentTime+7.5);
resume();
audioContext.resume()根据建议启动实际音频(或者更确切地说启动音频图计时,以便执行您计划的操作)。您的soundObj
对象没有start
方法。我建议将playSound
设置为soundObj
的一个属性,这样你就可以从外部访问它的start
方法。太好了,我已经知道我需要进一步研究基础知识,然后再尝试太雄心勃勃——“将playSound设置为soundObj的一个属性”会是什么样子?谢谢soundObj.source=playSound
然后您可以执行harp2.source.start(…)