我正在开发一个应用程序,我需要使用麦克风录制音频并将其发送到后端应用程序(tomcat 服务器)。
似乎发送太大的流会导致 Angular 疯狂并卡住我的浏览器。
为了录制我的音频文件,我使用 native 函数 RecorderWorkerFactory.getUserMedia(),它允许我获取 RecordBlob 对象。
之后,仍然在 Angular 中,我以 base64 编码提取音频内容,并使用 $resource 将其发送到后端应用程序。
后端应用程序正确接收数据并处理它,但此调用的回调永远不会执行,因为 Firefox 检测到无限循环并卡住。
但是,如果我继续运行该程序,很长一段时间后页面刷新就会过去。
var blob = $scope.audio.recordBlob;
if (blob) {
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
$scope.audioContent = reader.result;
$scope.sendMessage();
}
}
$scope.sendMessage = function(){
var outputStream = {
"audio": $scope.audioContent
};
$scope.sendIM(outputStream);
}
在这里,我通过 POST 发送 outputStream 到后面,并在回调中启动 loadData() 函数来重新加载我的 View 。
services.FileCreation= $resource(URI_SERVICE_CREATION, {}, {
'post' : urlEncodedFormPost
});
$scope.sendIM = function(fluxSortie) {
$services.FileCreation.post(angular.toJson(outputStream)).$promise.then(function(result){
$scope.loadData();
});
}
这是创建音频文件的 Java 代码:
private void createAudioFile(File file, byte[] content) throws IOException {
FileOutputStream stream = null;
try {
stream = new FileOutputStream(file.getPath());
IOUtils.write(content, stream);
} catch (IOException e) {
LOGGER.debug("creation failed");
} finally {
if (stream != null) {
stream.flush();
stream.close();
}
}
}
其中content是发送的base64字符串的转换。
<小时/>经过研究,我发现无限循环位于名为 shallowClearAndCopy() 的 native Angular 函数中,该函数发生在 Java 执行之后、回调之前。在此函数中,代码显然将音频字符串(base64 编码)的每个字符转换为对象属性,并对这些字符执行循环以删除它们。但这会导致很长的处理时间,Firefox 将其视为无限循环。
function shallowClearAndCopy(src, dst) {
dst = dst || {};
angular.forEach(dst, function(value, key) { // This is where it freezes, as dst contains all my base64 encoded data and iterate over each character of it (which is veeeeeery long !)
delete dst[key];
});
for (var key in src) {
if (src.hasOwnProperty(key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) {
dst[key] = src[key];
}
}
return dst;
}
是因为 angularjs 的性能(并且没有其他事情可做)吗? 或者我错过了一些造成无限循环的东西?或者我的回调定义有问题?
干杯!
最佳答案
我发现了,问题所在!
正是 Angular.
关于javascript - 使用 $resource 发送音频数据 (base64),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36578342/