【问题标题】:TypeError while using navigator.mediaDevices.getUserMedia使用 navigator.mediaDevices.getUserMedia 时出现 TypeError
【发布时间】:2019-02-05 10:02:19
【问题描述】:

我正在尝试将已弃用的 navigator.getUserMedia 替换为 navigator.mediaDevices.getUserMedia,但在尝试时出现此错误:

TypeError:无法设置未定义的属性“localStream”

这是我的 HTML:

   <video ref="videoPlayer" hide.bind="screenSharing" id="videoPlayer" autoplay muted></video>

我正在使用 Aurelia 环境,我的 javascript 是这样的:

attached() {

 if (!this.localStream) {
  this.getLocalMedia();
 }
}    

getLocalMedia() {
 let constraints: MediaStreamConstraints = {
   video: true,
   audio: true
 };

navigator.mediaDevices.getUserMedia(constraints)
  .then(function (stream) {
    console.log('Got mic+video stream', stream);
    this.localStream = stream;
    this.videoPlayer.srcObject = this.localStream;
    this.videoPlayer.srcObject = stream;
   })
   .catch (function (err) {
     console.error(err);
 });
// navigator.getUserMedia(constraints, (stream) => {
//   console.log('Got mic+video stream', stream);
//   this.localStream = stream;
//   this.videoPlayer.srcObject = this.localStream;
// },
//   (err) => {
//     console.error(err);
//   }
// );

  }

有人能想象出什么问题吗?我正在使用 Electron 创建一个应用程序。

我在网上看到其他人有类似的问题,所以我打印了流:

Got mic+video stream 
MediaStream {id: "EIGkOQeyfsaRpweVgRG8eQiZfiuJ2HV3QZqW", active: true,     onaddtrack: null, onremovetrack: null, onactive: null…}
active: true
id: "EIGkOQeyfsaRpweVgRG8eQiZfiuJ2HV3QZqW"
onactive: null
onaddtrack: null
oninactive: null
onremovetrack: null
__proto__: MediaStream

我也尝试在文档准备好时添加一个 EventListener,但我仍然遇到这个问题;如果我将注释部分与已弃用的 navigator.getUserMedia 一起使用,一切正常

【问题讨论】:

  • this 不是你想的那样。
  • 我在上课开始时有这个,抱歉我忘记了:export class App { localStream: MediaStream = null; videoPlayer: HTMLVideoElement;
  • 在调用navigator.mediaDevices.getUserMedia函数时,你确定你的constraints不是undefined吗?
  • @ymz 如果我打印约束我有这个:Object audio: true video: true __proto__: Object

标签: javascript node.js webrtc mediadevices


【解决方案1】:

您在旧的 navigator.getUserMedia 中使用了箭头函数,但在 navigator.mediaDevices.getUserMedia 中使用了经典函数。因为Andreas saidthis 不是你想的那样,这些风格之间的行为是不同的。

【讨论】:

  • 如果我改变了实际上它的样式;谢谢,我不明白安德烈亚斯的意思。谢谢你的澄清
猜你喜欢
  • 2019-04-05
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
  • 2021-04-10
相关资源
最近更新 更多