最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - <audio> tag to audioBuffer - is it possible? - Stack Overflow

programmeradmin1浏览0评论

My javascript-webApp first reads a short mp3 file and finds silence-gaps in it (for navigational purposes), then it plays the same mp3 file cueing it to start where one silence or another finishes. This differs from the usual webAudio scenario designed to grant access to audio data currently being played in the stream (not to the whole track).

To get my webApp to work I have to read/access the mp3 file twice:

  1. via XMLHttpRequest to read an entire MP3 file and put it in to an audioBuffer that I can subsequently decode using audioContext.decodeAudioData() - as explained here: Extracting audio data every t seconds
  2. by specifying the <audio> tag to allow me to play the file on demand specifying in milliseconds the cue/start point. Playing audio with Javascript?.

Q: Is there currently any way I might declare the <audio>tag first then somehow derive the audioBuffer directly from it, without resorting to XMLHttpRequest ?

I've read about createMediaElementSource but I can't see how to get an audioBuffer by using it.

My javascript-webApp first reads a short mp3 file and finds silence-gaps in it (for navigational purposes), then it plays the same mp3 file cueing it to start where one silence or another finishes. This differs from the usual webAudio scenario designed to grant access to audio data currently being played in the stream (not to the whole track).

To get my webApp to work I have to read/access the mp3 file twice:

  1. via XMLHttpRequest to read an entire MP3 file and put it in to an audioBuffer that I can subsequently decode using audioContext.decodeAudioData() - as explained here: Extracting audio data every t seconds
  2. by specifying the <audio> tag to allow me to play the file on demand specifying in milliseconds the cue/start point. Playing audio with Javascript?.

Q: Is there currently any way I might declare the <audio>tag first then somehow derive the audioBuffer directly from it, without resorting to XMLHttpRequest ?

I've read about createMediaElementSource but I can't see how to get an audioBuffer by using it.

Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked Apr 15, 2014 at 10:02 GavinBrelstaffGavinBrelstaff 3,0803 gold badges24 silver badges43 bronze badges 2
  • 1 Did you ever get this to work? – Kendall Commented Apr 8, 2018 at 0:33
  • 1 No I didn't properly give it a go – GavinBrelstaff Commented Apr 8, 2018 at 8:48
Add a ment  | 

1 Answer 1

Reset to default 5

When doing your first XHR, ask for a blob:

xhr.responseType = 'blob'

Then get an ArrayBuffer out of it:

var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function() {
    arrayBuffer = this.result;
};
fileReader.readAsArrayBuffer(blob);

and give that to decodeAudioData to get the AudioBuffer as usual. You can now do your processing.

Then, when your processing is done, give the blob to the tag, as a source, when you want to play it, it will work as usual:

 audio.src = window.URL.createObjectURL(blob);

You might need to prefix URL with the webkit vendor prefix, I can't remember if they implement the unprefixed version. Anyways, blobs are the way to go !

发布评论

评论列表(0)

  1. 暂无评论