我在用Javascript和Cordova制作的应用程序在Android上运行时遇到问题。该应用程序基本上可以捕获麦克风输入的实时音频,并且所有数据都被缓冲并显示在屏幕上。当音频低于特定水平时,该应用会显示警报。在Web浏览器中(在Firefox上测试),该方法有效,但是当我移植Web应用程序时,它仅在处理音频的代码部分之前显示警报。我读了一些AudioInput插件,但是我不知道如何使它工作。
使用网络应用程序,该应用程序可以很好地工作并满足所有目标,但是Android应用程序更重要,更有用。我尝试修改JS网络应用程序代码,但无法正常工作。
这是Web应用程序代码。我尝试在Cordova应用程序中使用,但没有任何反应:
var ventanas = [];
var MAX_VENTANAS = 7;
var webaudio_tooling_obj = function () {
alert("arrancando");
var audioContext = new AudioContext();
alert("audio is starting up ...");
var BUFF_SIZE = 16384;
var audioInput = null,
microphone_stream = null,
gain_node = null,
script_processor_node = null,
script_processor_fft_node = null,
analyserNode = null;
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (navigator.getUserMedia){
navigator.getUserMedia({audio:true},
function(stream) {
start_microphone(stream);
alert("Correct");
},
function(e) {
alert('Error capturing audio.');
}
);
} else { alert('getUserMedia not supported in this browser.'); }
// ---
function show_some_data(given_typed_array, num_row_to_display, label) {
var size_buffer = given_typed_array.length;
var index = 0;
var max_index = num_row_to_display;
console.log("__________ " + label);
for (; index < max_index && index < size_buffer; index += 1) {
console.log(given_typed_array[index]);
}
}
function process_microphone_buffer(event) { // invoked by event loop
var i, N, inp, microphone_output_buffer;
microphone_output_buffer = event.inputBuffer.getChannelData(0); // just mono - 1 channel for now
// microphone_output_buffer <-- this buffer contains current gulp of data size BUFF_SIZE
//show_some_data(microphone_output_buffer, 5, "from getChannelData");
var suma = 0;
for(var i=0; i<BUFF_SIZE; i++){
suma = suma + Math.abs(microphone_output_buffer[i]);
}
suma = suma / BUFF_SIZE;
document.getElementById("barra-progreso").style.width= Math.floor(suma*100)+"%";
if(ventanas.length>=MAX_VENTANAS){
ventanas.shift();
}
ventanas.push(suma);
//chart.render();
var sumaVentanas = 0;
for(var i=0; i<ventanas.length; i++){
sumaVentanas = sumaVentanas + ventanas[i];
}
sumaVentanas = sumaVentanas / (MAX_VENTANAS * BUFF_SIZE) * 1000000;
console.log(sumaVentanas)
document.getElementById("barra-progreso2").style.width= Math.floor(sumaVentanas)+"%";
}
function start_microphone(stream){
gain_node = audioContext.createGain();
microphone_stream = audioContext.createMediaStreamSource(stream);
microphone_stream.connect(gain_node);
script_processor_node = audioContext.createScriptProcessor(BUFF_SIZE, 1, 1);
script_processor_node.onaudioprocess = process_microphone_buffer;
microphone_stream.connect(script_processor_node);
}
}(); // webaudio_tooling_obj = function()
如果您可以向我推荐一门课程或一则帖子,以学习如何使我在Web应用程序代码中所做的事情与Cordova应用程序相似,我将不胜感激!对不起,我的英语不好,谢谢大家!