How to record using phone microphone on browsers?

瘦欲@ 提交于 2020-01-07 08:18:38

问题


I am trying to record a voice from browsers but it only works on chrome browser in my mac but the same doesn't work in any other browser. My goal is to record the voice from a phone from a browser.

I have been trying this below example but it doesn't works on mobile browsers. It opens up the microphone but doesn't stop recording and never plays it back.

I think the problem is somewhere around recorder.stop() which doesn't get called.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>

</script>
</head>

<body>
 <audio controls autoplay></audio>
<script type="text/javascript" src="recorder.js"> </script>

<input onclick="startRecording()" type="button" value="start recording" />
<input onclick="stopRecording()" type="button" value="stop recording and play" />

<script>
  var onFail = function(e) {
    console.log('Rejected!', e);
  };

  var onSuccess = function(s) {
    var context = new webkitAudioContext();
    var mediaStreamSource = context.createMediaStreamSource(s);
    recorder = new Recorder(mediaStreamSource);
    recorder.record();

    // audio loopback
    // mediaStreamSource.connect(context.destination);
  }

  window.URL = window.URL || window.webkitURL;
  navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

  var recorder;
  var audio = document.querySelector('audio');

  function startRecording() {
    if (navigator.getUserMedia) {
      navigator.getUserMedia({audio: true}, onSuccess, onFail);
    } else {
      console.log('navigator.getUserMedia not present');
    }
  }

  function stopRecording() {
    //alert("hello");
    recorder.stop();
    recorder.exportWAV(function(blob) {
      //audio.src = window.URL.createObjectURL(s);

      var url = URL.createObjectURL(blob);
                    audio.src = url;
                    audio.controls = true;
                    var hf = document.createElement('a');
                    hf.href = url;
                    hf.download = new Date().toISOString() + '.wav';
                    //upload(blob);   
                    audio.src = url;
    });

  }

  function upload(blobOrFile) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/upload.aspx', true);
    xhr.onload = function (e) {
        var result = e.target.result;
  };

    xhr.send(blobOrFile);
}
 </script>
 </body>
</html>

回答1:


Only Chrome supports as of now the AudioContext interface. Firefox also has an implementation but only in the Nightly build.

I'm not sure if it is supported by the mobile version of these browsers.



来源:https://stackoverflow.com/questions/21567348/how-to-record-using-phone-microphone-on-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!