admin管理员组文章数量:1278983
i wish load a mp3 local file and next using a library for processing, i cant use XMLHttpRequest, then i use the next code for read a local mp3 file:
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="carga();">
<input id="audio_file" type="file" accept="audio/*" />
<audio id="audio_player" />
<script>
function carga() {
audio_file.onchange = function(){
var files = this.files;
console.log(files);
alert(files);
var file = URL.createObjectURL(files[0]);
console.log(file);
alert(file);
audio_player.src = file;
audio_player.play();
};
}
</script>
</body>
</html>
but XMLHttpRequest return ArrayBuffer, and my code return a FileList, i need a ArrayBuffer for my next processing, how i can load a local mp3 file as a ArrayBuffer?
i wish load a mp3 local file and next using a library for processing, i cant use XMLHttpRequest, then i use the next code for read a local mp3 file:
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="carga();">
<input id="audio_file" type="file" accept="audio/*" />
<audio id="audio_player" />
<script>
function carga() {
audio_file.onchange = function(){
var files = this.files;
console.log(files);
alert(files);
var file = URL.createObjectURL(files[0]);
console.log(file);
alert(file);
audio_player.src = file;
audio_player.play();
};
}
</script>
</body>
</html>
but XMLHttpRequest return ArrayBuffer, and my code return a FileList, i need a ArrayBuffer for my next processing, how i can load a local mp3 file as a ArrayBuffer?
Share Improve this question edited Aug 24, 2015 at 0:51 mido 25.1k15 gold badges99 silver badges122 bronze badges asked Aug 23, 2015 at 23:15 gurrumogurrumo 832 silver badges10 bronze badges 01 Answer
Reset to default 11On modern Browser you can use a FileReader
and its method readAsArrayBuffer
.
document.querySelector('input').onchange = function(){
var fileReader = new FileReader;
fileReader.onload = function(){
var arrayBuffer = this.result;
snippet.log(arrayBuffer);
snippet.log(arrayBuffer.byteLength);
}
fileReader.readAsArrayBuffer(this.files[0]);
var url = URL.createObjectURL(this.files[0]);
audio_player.src = url;
audio_player.play();
};
<!-- Provides the `snippet` object, see http://meta.stackexchange./a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<input id="audio_file" type="file" accept="audio/*" />
<audio id="audio_player" />
本文标签: javascriptLoading mp3 as ArrayBuffer using local file for Web AudioStack Overflow
版权声明:本文标题:javascript - Loading mp3 as ArrayBuffer using local file for Web Audio - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741262687a2367912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论