admin管理员组文章数量:1421018
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:
- via
XMLHttpRequest
to read an entire MP3 file and put it in to an audioBuffer that I can subsequently decode usingaudioContext.decodeAudioData()
- as explained here: Extracting audio data every t seconds - 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:
- via
XMLHttpRequest
to read an entire MP3 file and put it in to an audioBuffer that I can subsequently decode usingaudioContext.decodeAudioData()
- as explained here: Extracting audio data every t seconds - 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.
- 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
1 Answer
Reset to default 5When 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 !
本文标签: javascriptltaudiogt tag to audioBufferis it possibleStack Overflow
版权声明:本文标题:javascript - <audio> tag to audioBuffer - is it possible? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745338686a2654161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论