0

已编辑 - 在帖子底部更新

我正在使用phonegap为android构建一个基于web的应用程序,我遇到了这个问题,在初始化getusermedia之后,当我使用音量调低/调高按钮时,音量控制是用于通话而不是扬声器,即使我没有' t开始新的录音...此外,我注意到手机实际上在应用程序运行时认为它在通话中,例如:我启动我的应用程序,然后我打开whatsapp并尝试录制消息正在发送的语音消息取消。

我知道下一段是问题(我评论了它,没有问题)

/***recording audio block***/
function audioRecordingInit() {
navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
    console.log('getUserMedia supported.');

    var constraints = {audio: true};
    var chunks = [];

    var onSuccess = function (stream) {
        $.globals.mediaRecorder = new MediaRecorder(stream);
        $.globals.mediaRecorder.onstop = function(e) {
            console.log("data available after MediaRecorder.stop() called.");
            var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });
            chunks = [];
            if(!$("#recordBtn").hasClass("private"))
                $.globals.lastRecorded.audio.src = window.URL.createObjectURL(blob);
            else
                $.globals.lastRecordedPrivate.audio.src = window.URL.createObjectURL(blob);
            console.log("audio created");


        };
        $.globals.mediaRecorder.ondataavailable = function(e) {
            chunks.push(e.data);
        }
    };
    var onError = function (err) {
        console.log('The following error occured: ' + err);
    };

    navigator.getUserMedia(constraints, onSuccess, onError);// I think this is the problem
}
else{
    console.log('getUserMedia not supported on your browser!');
}
/***end of recording audio block***/
}

设备准备好后调用此函数

我还在下面添加了开始录制和停止录制功能

function startRecording(event) { 
  document.getElementById("recordingTime").innerText = "00:00";
  $.globals.mediaRecorder.start();
  console.log($.globals.mediaRecorder.state);
  console.log("recorder started");
  $.globals.timerInterval = setInterval(function () {
      $.globals.sec += 1;
      if ($.globals.sec == 60) {
          $.globals.min++;
          $.globals.sec = 0;
      }
      if ($.globals.min < 10) {
          if ($.globals.sec < 10)
              $.globals.timeText = "0" + $.globals.min + ":0" + $.globals.sec;
        else
            $.globals.timeText = "0" + $.globals.min + ":" + $.globals.sec;
    }
    else if ($.globals.sec < 10)
        $.globals.timeText = min + ":0" + $.globals.sec;
    else
        $.globals.timeText = min + ":" + $.globals.sec;
    document.getElementById("recordingTime").innerText = $.globals.timeText;
}, 1000);
   }
    function stopRecording() {
  if($(".circleNav").hasClass("recording"))
      $(".circleNav").toggleClass("recording");
  $.globals.currentState="Recorded";
  console.log($.globals.mediaRecorder.state);
  if($.globals.mediaRecorder.state=="inactive"){
      $.globals.mediaRecorder.start();
   }
  $.globals.mediaRecorder.stop();

console.log("recorder stopped");
clearInterval($.globals.timerInterval);

  }

startRecording 在 touchstart 上录制按钮时开始 stopRecording 当 touchend on record 按钮时调用 谢谢你的帮助

更新

冲突与麦克风有关,因为流始终是实时的,而不仅仅是在录制时。现在它工作正常,但仍然需要在通话时禁用录音按钮,否则它会发生冲突并可能导致应用程序崩溃或将麦克风与所有设备断开连接,甚至可能断开连接。

4

1 回答 1

1

好的,所以在阅读了大量有关 MediaRecorder、MediaStream 和 MediaStreamTrack 的信息之后。我发现了问题,音频流保持活跃并且正在使用麦克风,因此拒绝我访问whatsapp中的电话和语音消息。我将在下面添加我的解决方案:

function audioRecordingInit() {
navigator.mediaDevices.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
    console.log('getUserMedia supported.');
    navigator.getUserMedia({audio:true}, onSuccessMedia, onErrorMedia);
    return true;
}
else{
    console.log('getUserMedia not supported on your browser!');
    return false;
}
}

请注意,我让这个函数返回一个布尔值,并将成功和错误函数分开。

 function onSuccessMedia(stream) {
var chunks = [];
$.globals.mediaStream=stream;
console.log(stream);
$.globals.mediaRecorder = new MediaRecorder(stream);
$.globals.mediaRecorder.onstop = function(e) {
    console.log("data available after MediaRecorder.stop() called.");
    var blob = new Blob(chunks, { 'type' : 'audio/mp3; codecs=opus' });
    chunks = [];
    if(!$("#recordBtn").hasClass("private"))
        $.globals.lastRecorded.audio.src = window.URL.createObjectURL(blob);
    else
        $.globals.lastRecordedPrivate.audio.src = window.URL.createObjectURL(blob);
    console.log("audio created");
};
$.globals.mediaRecorder.ondataavailable = function(e) {
    chunks.push(e.data);
};
$.globals.mediaRecorder.start();
console.log("recording start");
}
function onErrorMedia(err) {
   console.log('The following error occured: ' + err);
}

在这里,我将 mediaRecorder.start() 移至 onSuccess 函数,而不是在开始录制内部...

我很好地将开始录制功能更改为

 function startRecording(event) {
     if(audioRecordingInit()) {
        //here is all of the visual changes
      }
      }

就是这样,一切正常

于 2016-12-10T12:44:20.503 回答