Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  88] [ 3]  / answers: 1 / hits: 17018  / 12 Years ago, sat, february 16, 2013, 12:00:00

I am trying to load an audio buffer from an URL, and then to play it. I got most of the code form this HTML5 Rocks Tutorial.



var request = new XMLHttpRequest();
request.open('GET', $(this).attr('data-url'), true);
request.responseType = 'arraybuffer';

request.onload = function() {
console.log(request);
context.decodeAudioData(request.response, function(buffer) {
console.log(buffer);
$('#play').click(function() {
var source = context.createBufferSource();
source.connect(context.destination);
source.noteOn(0);
}).removeAttr('disabled');
}, function(err) { console.log(err); });
};
request.send();


However, then I press the #play button, nothing happens. source.noteOn(0) is called, I checked it using the debugger. And all of the objects are properly loaded and created, but I hear no sound.



Also, as it seems, I would need to rebuild a complete player with all controls when I am using this approach. What I'd like to do, to save work and to ensure this works better, is to put the buffer into an <audio/>, so it can be played there.



I know there is audio.src for putting the file name in there, but I need to use the audio buffer. I've tried



audio.src = buffer;
audio.load()


But that did not work.



Any info there?


More From » html

 Answers
6

If you just want to play audio-files, you probably want to use the <audio> tag for sake of simplicity. (and for not being limited to webkit browsers).



In your example you do not set the buffer of your buffer-source node:

if you want to keep the overall structure, you can simply add the line source.buffer = buffer, like:



context.decodeAudioData(request.response, function(buffer) {
$('#play').click(function() {
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.noteOn(0);
}).removeAttr('disabled');
}, function(err) { console.log(err); })


(your code's readability would improve by separating the audio decoding from the event-handling).



your other question on audio.src:

you should set audio.src to the URL of the audio file.


[#80193] Thursday, February 14, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;