我希望加载一个mp3本地文件,然后使用库进行处理,我不能使用XMLHttpRequest,然后我使用下一个代码来读取本地mp3文件:
if(getSupportFragmentManager.findFragmentById(R.id.mainfragment) == null) {
getSupportFragmentManager()
.beginTransaction
.add(R.id.mainfragment, new MainFragment())
.commit();
}
//...This can be in onCreate(), or inside a method, to access MainFragment's methods
MainFragment frag = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.mainfragment);
if (frag != null) frag.callYourMethodHere();
但XMLHttpRequest返回ArrayBuffer,我的代码返回一个FileList,我需要一个ArrayBuffer进行下一次处理,如何将本地mp3文件作为ArrayBuffer加载?
答案 0 :(得分:8)
在modern Browser上,您可以使用FileReader
及其方法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.com/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" />
&#13;