我为我的Web应用程序准备了以下代码,我需要进行一些改进,它可以以某种方式工作,但结果非常缓慢:
该代码背后的思想是:
更新(简化代码):
private String data = "";
// This function renders the waveform in the page, has been tested to
// render properly and smoothly by passing random double value at 50ms interval
public void updateWave(String waveValue){
wave.renderWaveOnFly(Double.parseDouble(waveValue));
}
public final native void waveIt()/*-{
var instance = this;
$wnd._waver = setInterval(function(){
// Get the current buffer from the flash interface
// Note it fetches everything in the buffer
var newData = $wnd.Recorder.audioData().toString();
var strData = newData.toString();
var arr = strData.split(',');
var arrEl = arr.pop();
[email protected]::updateWave(Ljava/lang/String;)(arrEl.toString());
//console.log(arrEl);
}
,50);
}-*/;
// This function renders the waveform from math function
// and the waveform is smooth and the UI is still responsive
public final native void waveItByRandomValue()/*-{
var instance = this;
$wnd._waver = setInterval(function(){
var arrEl = Math.cos(i++/25) - 0.2 + Math.random() * 0.3;
[email protected]::updateWave(Ljava/lang/String;)(arrEl.toString());
}
,50);
}-*/;
public native void renderWaveOnFly(Double _data)/*-{
var data = $wnd.data;
data.push(_data);
$wnd._waveform.update({
data: data
});
}-*/;
waveIt()
是从闪存接口(interface)读取缓冲区的功能(该接口(interface)从麦克风获取数据)。对于演示,我将麦克风录音机设置为在触发时录制10秒,然后在录制开始时waveIt()
,在10秒钟后调用clearInterval($wnd._waver)
这个问题:
waveIt()
函数确实很慢,即,UI在运行时不响应,并且渲染速度非常慢waveItByRandomValue()
相比,后者可以快速渲染,并且在运行此功能时UI仍会响应关于如何正确执行此工作,我没有策略。
要实时观看我的项目,请参见:http://bitly.com/XGboA1
我也确实在Google网上论坛中解释了更多此问题:http://bitly.com/SqSZVl
最佳答案
这可能是解决方案的一部分,类型数组:
http://www.html5rocks.com/en/tutorials/webgl/typed_arrays/