简体   繁体   中英

call a function from navigator.mediaDevices.getUserMedia

I am trying to call a function from within navigator.mediaDevices.GetUserMedia and it ain't working.

This is what I have

navigator.mediaDevices.getUserMedia({audio: true}, startUserMedia, function(e) {
   __log('No live audio input: ' + e);
});

This is my function

function startUserMedia(stream) {
    var input = audio_context.createMediaStreamSource(stream);
    __log('Media stream created.' );
    __log("input sample rate " +input.context.sampleRate);
    __log('Input connected to audio context destination.');

    recorder = new Recorder(input, {
                  numChannels: 1
                });
    __log('Recorder initialised.');
  }

I'm trying to update this call, since before it was:

navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
     __log('No live audio input: ' + e);
});

and guess what, that works. But this new "mediaDevices", I can't make it work somehow. It says:

Uncaught TypeError: recorder is undefined

It is not making the call to the "startUserMedia" function. If I add a "alert("hello")" inside the function, it doesn't executes.

This works though:

 navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
      startUserMedia();
}).catch(function(err) {});

but it doesn't recognizes my recorder

recorder = new Recorder(input, { numChannels: 1 });

Anyone can lend a hand?

Found the answer thanks to Derek there.

  window.AudioContext = window.AudioContext || window.webkitAudioContext;
  navigator.getUserMedia = (navigator.getUserMedia ||
    navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia ||
    navigator.msGetUserMedia);
    audio_context = new AudioContext;
    __log('Audio context set up.');

    if (navigator.mediaDevices) { // if navigator.mediaDevices exists, use it
        navigator.mediaDevices.getUserMedia({audio: true}).then(startUserMedia, function(e) {
          __log('No live audio input: ' + e);
        });
    } else {
        navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
          __log('No live audio input: ' + e);
        });
    }

This way I can check both if the browser supports getUserMedia or not. Either way, it fires the prompt that asks the user for the browser's media permission.

The StartUserMedia function gets called in both instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM