Is it possible to first upload an mp3 file with the html5 drag and drop upload system, and then play it with webkit's audio API (http://chromium.googlecode.com/svn/trunk/samples/audio/index.html) without submitting a form (in Google Chrome)? Is it possible to do in FF with Mozilla's audio API? If so, how? Also, are there any tutorials in existance for webkit's API? I have not been able to find any.
2 Answers
The basic process you need to follow is
- Capture the files using Drag and Drop Files
- Posting the files in a Form Data Object
- Respond with the URL of the audio item you want to play
- Play the Audio using the Audio API
This jsFiddle allows you to drag an audio file into an area and it will then play that file.
You should be able to use the JavaScriptAudioNode's onaudioprocess event to get the current amplitude.
Edit:
Based on what JaapH said I had a look into this again. The processor was used to get an appropriate event to render the canvas. So it is not really required. This jsFiddle does the same as below. However, it uses requestAnimationFrame instead of the processor.
Here is the old code, see the fiddle above for the version using request animation frame:
var context = new (window.AudioContext || window.webkitAudioContext)();
var source;
var processor;
var analyser;
var xhr;
function initAudio(data) {
source = context.createBufferSource();
if(context.decodeAudioData) {
context.decodeAudioData(data, function(buffer) {
source.buffer = buffer;
createAudio();
}, function(e) {
console.log(e);
});
} else {
source.buffer = context.createBuffer(data, false /*mixToMono*/);
createAudio();
}
}
function createAudio() {
processor = context.createJavaScriptNode(2048 /*bufferSize*/, 1 /*num inputs*/, 1 /*numoutputs*/);
processor.onaudioprocess = processAudio;
analyser = context.createAnalyser();
source.connect(context.destination);
source.connect(analyser);
analyser.connect(processor);
processor.connect(context.destination);
source.noteOn(0);
setTimeout(disconnect, source.buffer.duration * 1000);
}
function disconnect() {
source.noteOff(0);
source.disconnect(0);
processor.disconnect(0);
analyser.disconnect(0);
}
function processAudio(e) {
var freqByteData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(freqByteData);
console.log(freqByteData);
}
function handleResult() {
if (xhr.readyState == 4 /* complete */) {
switch(xhr.status) {
case 200: /* Success */
initAudio(request.response);
break;
default:
break;
}
xhr = null;
}
}
function dropEvent(evt) {
evt.stopPropagation();
evt.preventDefault();
var droppedFiles = evt.dataTransfer.files;
//Ajax the file to the server and respond with the data
var formData = new FormData();
for(var i = 0; i < droppedFiles.length; ++i) {
var file = droppedFiles[i];
files.append(file.name, file);
}
xhr = new XMLHttpRequest();
xhr.open("POST", 'URL');
xhr.onreadystatechange = handleResult;
xhr.send(formData);
}
function dragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
return false;
}
var dropArea = document.getElementById('dropArea');
dropArea.addEventListener('drop', dropEvent, false);
dropArea.addEventListener('dragover', dragOver, false);
I hope this helps

- 1
- 1

- 590
- 1
- 5
- 13
-
That would work fine, but I NEED to play it with webkit's audio API and/or Mozilla's. Or, if there was a way to get the current amplitude of something playing in an audio tag, that would work too. Also, don't assume people are using jquery. – invisible bob Oct 14 '11 at 19:26
-
Ok. I have taken out jquery code and update the answer to use the Audio API. This [jsFiddle](http://jsfiddle.net/gaJyT/11/) has all the Audio API code but it uses the file reader instead of AJAX. There is a lot more in the code now. Let me know if I need to explain it better. – John McKim Oct 15 '11 at 15:48
-
When I run the jsfiddle example the sound is distorted this is due that both the processor and source are connected to context.destination. – JaapH Feb 27 '12 at 02:23
-
I have not been able to reproduce any problems with distorted sounds. However, if you are only looking to animate the frequency data, I added an edit that shows using requestAnimationFrame instead of the processor. – John McKim Feb 27 '12 at 08:30
-
1I've updated the fiddle, it seems to not work due to audio api updates. This should make it functional: http://jsfiddle.net/gaJyT/182/ – DebugXYZ Jan 22 '17 at 08:35
When I run the jsfiddle example the sound is distorted this is due that both the processor and source are connected to context.destination. To get it to work I removed the line source.connect(context.destination);
and in the processAudio
function I added code to copy the input samples to the output
var inL = e.inputBuffer.getChannelData(0);
var inR = e.inputBuffer.getChannelData(1);
var outL= e.outputBuffer.getChannelData(0);
var outR =e.outputBuffer.getChannelData(1);
var n = inL.length;
for (var i = 0; i < n; i++) {
outL[i] = inL[i];
outR[i] = inR[i];
}

- 391
- 2
- 6