Javascript: Audio from local file not playing without HTML "<audio>" element present -
now 1 has me stumped, hoping see if spot doing things incorrectly.
so essentially, have page 2 elements. 1 html5 file handler, , second button. when user selects file respond onchange event generated, decoding audio , constructing buffer used. know there html5 audio tag, going utility needs able break file manageable chunks.
i have done several tests , have found audio decode myself play after audio element on page has been played. have absolutely no idea causing behavior, since have studied several examples online on playing audio. include audio engine below.
just clarify, handled code.
thank you.
"use strict"; var audiofile = function (){ this.length = 0; // number of samples this.duration = 0; // time in seconds this.samplerate = 0; // sample rate this.channels = 0; // number of channels this.data = []; //audio/waveform data }; var audioctx = null; class audioengine { constructor (){ // of necessary audio control variables if(!audioctx){ window.audiocontext = window.audiocontext || window.webkitaudiocontext; audioctx = new audiocontext(); } // hold audio data waiting played this.buffer = null; // hold decoded audio file upon completion this.decodedfile = new audiofile(); // automatically create buffer upon finished decoding? this.autocreatebuffer = true; // specify if want have function recieve // decoded audio, i.e. completioncallback(decodedfile); this.completioncallback = null; } // decode audio file filecallback (event){ console.log("file callback"); this.buffer = null; var reader = new filereader(); var file = event.target.files[0]; reader.onload = this.loadcallback.bind(this); reader.readasarraybuffer(file); } // called filecallback after file has been loaded loadcallback (file){ console.log("load callback"); var raw = file.target.result; audioctx.decodeaudiodata(raw, this.decodecallback.bind(this)); } // called loadcallback after file has been decoded decodecallback (data){ console.log("decode callback"); var audiotemp = new audiofile(); audiotemp.length = data.length; audiotemp.duration = data.duration; audiotemp.samplerate = data.samplerate; audiotemp.channels = data.numberofchannels; var arr = []; for(var = 0; < data.numberofchannels; i++){ arr.push(new float32array(data.length)); data.copyfromchannel(arr[i], i); } audiotemp.data = arr.slice(0); this.decodedfile = audiotemp; if(this.autocreatebuffer){ var buffer = audioctx.createbuffer(audiotemp.channels, audiotemp.length, audiotemp.samplerate); var samples; for(var c = 0; c < audiotemp.channels; c++){ samples = buffer.getchanneldata(c); for(var = 0; < audiotemp.length; i++){ samples[i] = this.decodedfile.data[c][i]; } } this.buffer = buffer; } if(this.completioncallback){ this.completioncallback(audiotemp); } } // play data in buffer play(){ if(this.buffer){ var source = audioctx.createbuffersource(); var tmp = this.buffer.getchanneldata(0); source.buffer = this.buffer; source.connect(audioctx.destination); source.start(0); console.log("play"); } } }
okay, have figured out seems problem. code in testing phase , seems have console being open on firefox messes sound being played. although, not entirely consistent how behaves, @ least know general cause of problem having.
in other words, "audio" elements have no problems whether console open or not, there seems undefined behavior when opening/minimizing console javascript controlled audio. however, behaves expected when console closed.
Comments
Post a Comment