我有一个对象,它有一个loadFile()
方法来分析一个wav文件。我一直在对象之外使用FileReader
,并从读取的文件中传递loadFile()
,ArrayBuffer
并让它从那里接管。我想要有可能的很多对象,所以我想在loadFile()
方法中打包FileReader
,所以我不必为每个对象处理外部的所有阅读器代码。问题是,我不知道这将如何工作。这里有一个可视代码:在对象内部使用FileReader
function MyObject() {
this.property;
this.anotherProp;
this.data = new Array(); // array to be filled with PCM data for processing
}
// pass a File object
MyObject.prototype.loadFile = function(file) {
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function(event) {
var buffer = event.target.result;
// do lots of stuff with buffer and eventually fill up this.data[]
}
}
MyObject.prototype.doProcessing = function() {
// process this.data[]
}
var file; // a File object I grabbed from somewhere
var myObj = new MyObject();
myObj.loadFile(file);
myObj.doProcessing();
loadFile()
会发生什么情况?在我有myObj.data[]
的任何数据之前,loadFile
会返回吗?还是等待reader.onload
才能退火?如果这是错误的做法,我该怎么做? Aslo,如果我想loadFile()
返回false
如果内部reader.onload
失败?
可能的解决方案:http://jsfiddle.net/N6vnU/2/
目前的解决方案:我结束了移动文件的解析函数出对象的成网的工作人员,发现FileReaderSync
。现在,我将文件发送给网络工作人员,并在返回后创建一个包含结果的对象。
add'var self = this;'在读取器的onload()函数之上,self.data = onload()中的缓冲区,然后在reader.onload()的末尾调用self.doProcessing(),你就是金... – dandavis
谢谢,这部分是led我到我目前的解决方案 –