i got following code loading , playing .wavfile on chrome browser:
var songbuffer = null; // fix prefixing window.audiocontext = window.audiocontext || window.webkitaudiocontext; var context = new audiocontext(); function loadsound(url) { var request = new xmlhttprequest(); request.open('get', url, true); request.responsetype = 'arraybuffer'; // decode asynchronously request.onload = function() { context.decodeaudiodata(request.response, function(buffer) { songbuffer = buffer; }); } request.send(); } // fix prefixing window.audiocontext = window.audiocontext || window.webkitaudiocontext; var context = new audiocontext(); function playsound(buffer) { var source = context.createbuffersource(); // creates sound source source.buffer = buffer; // tell source sound play source.connect(context.destination); // connect source context's destination (the speakers) source.start(0); // play source } loading being triggered by: <button onclick="loadsound('audio/sound.wav')">load sound</button> , file loads fine when generate event.
and playing handled : <button onclick="playsound()">play sound</button>.
console.log tells me, however:
uncaught typeerror: failed set 'buffer' property on 'audiobuffersourcenode': provided value not of type 'audiobuffer'.
what going on?
you not passing playsound function, means line
source.buffer = buffer; is interpreted as
source.buffer = undefined; since have songbuffer variable in scope accessible playsound function, can remove buffer parameter , do
source.buffer = songbuffer; so, wrap - keep is, edit playsound function this:
function playsound() { var source = context.createbuffersource(); // creates sound source source.buffer = songbuffer; // tell source sound play source.connect(context.destination); // connect source context's destination (the speakers) source.start(0); // play source }
Comments
Post a Comment