Loading an Audio buffer and play it using the audio tag

随声附和 提交于 2019-12-01 20:27:08

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!