getUserMedia() in JavaScript normalizes across browsers. Illegal Invocation

江枫思渺然 提交于 2019-12-01 03:18:10

Apparently, it needs the context to be the navigator object, for whatever reason. I've noticed the same thing with console.log - it needs the console context.

When you do navigator.getUserMedia, the context is automatically set to navigator, as with any other time you invoke a method on an object. If you just do getUserMedia though, the context (by default) is global, and so it throws an Illegal Invocation error.

If you still want to save it as a variable, you can call it with the correct context at runtime:

var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// now I try to invoke it with some parameters:  
getUserMedia.call(navigator, ...)

You could also use bind to save the context with the variable, so that you don't have to call it each time:

var getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia).bind(navigator);
// now I try to invoke it with some parameters:  
getUserMedia(...)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!