2014-10-31 38 views
2

所以目前有兩個聲音加載到緩衝區中,然後連接到兩個聲源。如何將BufferLoader中的兩個聲音命名爲「kick」和「hihat」,然後使用kick.start(0)播放它們。我知道這一定很容易,但我無法通過搜索找到任何東西。Web Audio API如何命名聲音

window.onload = init; 
var context = new AudioContext(); 
var bufferLoader; 

function init() { 

    bufferLoader = new BufferLoader(
    context, 
    [ 
     'kick.wav', 
     'hihat.wav', 
    ], 
    finishedLoading 
    ); 

    bufferLoader.load(); 
} 

function finishedLoading(bufferList) { 

    var source1 = context.createBufferSource(); 
    var source2 = context.createBufferSource(); 
    source1.buffer = bufferList[0]; 
    source2.buffer = bufferList[1]; 

    source1.connect(context.destination); 
    source2.connect(context.destination); 
    source1.start(0); 
    source2.start(0); 
}  

回答

1

,如果你喜歡,你可以使用我寫我自己的抽象:

function audioFileLoader(fileDirectory) { 
    var soundObj = {}; 
    soundObj.fileDirectory = fileDirectory; 

    var getSound = new XMLHttpRequest(); 
    getSound.open("GET", soundObj.fileDirectory, true); 
    getSound.responseType = "arraybuffer"; 
    getSound.onload = function() { 
     audioContext.decodeAudioData(getSound.response, function(buffer) { 
      soundObj.soundToPlay = buffer; 

     }); 
    } 

    getSound.send(); 


    soundObj.play = function() { 
     var playSound = audioContext.createBufferSource(); 
     playSound.buffer = soundObj.soundToPlay; 
     playSound.connect(audioContext.destination) 
     playSound.start(audioContext.currentTime) 
    } 

    return soundObj; 

}; 

var snare = audioFileLoader("snare.mp3"); 


snare.play() 
相關問題