我正在使用jwegener's recorder.js进行音频录制。我很好奇是否有可能在记录器运行时从javascript中轮询flash,以获得音频的“帧”以构建波形。
在我想象的伪代码中:
function record() {
Recorder.Record();
setInterval(function(){
Recorder.fetchCurrentAudioSnapshot();
}, 50);
}理想情况下,这会给出一个数值,我可以用它来绘制波形。
感谢您能给出的任何想法。
发布于 2012-11-06 02:00:22
我想通了。
我做的第一件事是将示例javascript放入一个对象中。下面是我正在使用的记录函数:
record: function() {
this.data = [];
this.poll = '';
Recorder.record({
start: function () {
alert("recording starts now. press stop when youre done. and then play or upload if you want.");
},
progress: function (milliseconds) {
document.getElementById("time").innerHTML = Voiceover.timecode(milliseconds);
}
});
this.waveform = new Waveform({
container: document.getElementById("voiceover_waveform"),
interpolate: false
});
var ctx = this.waveform.context;
var gradient = ctx.createLinearGradient(0, 0, 0, this.waveform.height);
gradient.addColorStop(0.0, "#f60");
gradient.addColorStop(1.0, "#ff1b00");
this.waveform.innerColor = gradient;
var i=0;
this.poll = setInterval(function() {
Voiceover.data.push(Recorder.PollAudioBuffer());
Voiceover.waveform.update({
data: Voiceover.data
});
}, 70);
},这将初始化波形,并设置70ms的间隔来轮询记录器,以获得新的数据点,并将其绘制在波形上。
在recorder.js中,我添加了以下函数:
PollAudioBuffer: function() {
try {
var poll = this.flashInterface().pollAudioBuffer();
console.log(poll);
if(poll < 0.15) {
// Messing around with the math a bit to improve the effect.
// The final result just has to be between 0 and 1.
poll = Math.floor((Math.random()*1.5)+1)/10;
}
return poll;
}
catch(error) {
console.log(error);
}
},现在让我们来看看动作脚本。在Recorder.as中,我添加了我的pollAudioBuffer()函数,现在这个函数实际上有点命名错误。
protected function pollAudioBuffer():Number
{
return (microphone.activityLevel*2)/100;
}要使函数工作,需要在Recorder.as中添加对addExternalInterfaceCallbacks()的回调引用
ExternalInterface.addCallback("pollAudioBuffer",this.pollAudioBuffer);这些更改使我可以即时创建波形。希望这能对其他人有所帮助。
https://stackoverflow.com/questions/13197601
复制相似问题