这是代码的简历:

var client = new BinaryClient('ws://localhost:9001');
var context = null;
var store_data  = null;
//(.....)
if (!navigator.getUserMedia)
      navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia || navigator.msGetUserMedia;

if (navigator.getUserMedia) {
  navigator.getUserMedia({audio:true}, success, function(e) {
    alert('Error capturing audio.');
  });
} else alert('getUserMedia not supported in this browser.');

function success(e) {
      audioContext = window.AudioContext || window.webkitAudioContext;
      context = new audioContext();
      audioInput = context.createMediaStreamSource(e);
      var bufferSize = 2048;
      store_data = context.createScriptProcessor(bufferSize, 1, 1);
      //(...)
}

//(....)
client.on('open', function() {
    console.log("createStream");
    Stream = client.createStream(command_list);
    var recording = false;

    window.startRecording = function() {
      document.getElementById("startbutton").disabled = true;
      document.getElementById("stopbutton").disabled = false;

      recording = true;
      window.Stream.resume();
    }

    window.stopRecording = function() {
      document.getElementById("startbutton").disabled = false;
      document.getElementById("stopbutton").disabled = true;

      recording = false
      //window.Stream.end();
      window.Stream.pause();
    }

    store_data.onaudioprocess = function(e){ //<---line of the error
        if(!recording) return;
        console.log ('recording');
        var left = e.inputBuffer.getChannelData(0);
        window.Stream.write(convertoFloat32ToInt16(left));
      }
//(..events generated from server..)


在chrome中,我的代码可以正常工作。在Mozilla中,我总是收到错误“存储数据未定义”。知道为什么吗?因为我将store_data声明为全局,并且当getusermediasucess时,值已更改。

最佳答案

在不知道调用什么功能的情况下,很难确切地说出,但是我敢肯定,您希望您的success侦听器取决于成功函数的运行。

我不知道它将如何影响其余省略的代码,但只有在成功函数运行并且您确定已定义client.on('open')时,我才连接BinaryClient

function success() {
  var client = new BinaryClient('ws://localhost:9001');
  var context = null;
  var store_data  = null;

  // do the original success code here

  // now create that listener.
  client.on('open', function() {
    // do original code here
  });
}

// you probably have a line of code that looks like this
navigator.getUserMedia({}, success);


将所有代码移到成功函数中可能会起作用,但是这样做并不理想。一旦流程开始工作,我建议通过将每个逻辑位分解成自己的功能来重构代码。

09-19 16:58