angular - 获取 MediaStreamError { 名称 : "AbortError", 消息 : "Starting video failed", 约束 : "", 堆栈 : "" } on one PC, 但不在另一个上

标签 angular express logitech

我在台式电脑上收到 MediaStreamError { name: "AbortError", message: "Starting video failed", constraint: "", stack: ""},但在我的笔记本电脑上没有。注意:两台 PC 都运行 Windows 10 并使用相同的代码库。

该应用程序在我使用 Firefox(带有 USB 2.0 HD UVC 网络摄像头)的笔记本电脑上运行良好,但在我的台式电脑上,无论是使用 Firefox、Edge 还是 Chrome,我仍然会遇到错误。我的台式电脑摄像头是罗技 (Logitech HD WebCam C270),我在另一个帖子上看到同样的错误 Firefox 54 (ubuntu 14.04): Twilio video failed getUserMedia其他人 (@Roger Walsh) 在使用罗技相机时也遇到了同样的错误。

代码如下: 前端( Angular View )

<div class="camera">
  <video #videoRef id="video" [(ngModel)]="video" (canplay)="setVideo()" name="video" ngDefaultControl>Video stream not available.</video>
  <button #startbuttonRef id="startbutton" [(ngModel)]="startbutton" (click)="takePicture()" name="startbutton" ngDefaultControl>Take photo</button>
</div>

<canvas #canvasRef id="canvas" [(ngModel)]="canvas" name="canvas" ngDefaultControl style="display:none"></canvas>
<div class="output">
  <img #photoRef id="photo" [(ngModel)]="photo" name="photo" ngDefaultControl alt="The screen capture will appear in this box.">
</div>

前端( Angular 组件)

import { Component, Input, OnInit, forwardRef, ViewChild, ElementRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-capture-image',
  templateUrl: './capture-image.component.html',
  styleUrls: ['./capture-image.component.scss'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => CaptureImageComponent),
      multi: true
    }
  ]
})
export class CaptureImageComponent implements OnInit {
  @ViewChild('videoRef') videoRef: ElementRef;
  @ViewChild('canvasRef') canvasRef: ElementRef;
  @ViewChild('photoRef') photoRef: ElementRef;
  @ViewChild('startbuttonRef') startbuttonRef: ElementRef;
  streaming = false;
  width = 320;
  height = 0;

  constructor(private http: HttpClient) { }

  ngOnInit() {
    navigator.mediaDevices.getUserMedia({video: true, audio: false})
      .then((stream) => {
        this.videoRef.nativeElement.srcObject = stream;
        this.videoRef.nativeElement.play();
      })
      .catch(function(err) {
        console.log(err);
      });
      this.clearPhoto();
  }


  setVideo() {
    if (!this.streaming) {
      this.height = this.videoRef.nativeElement.videoHeight/ (this.videoRef.nativeElement.videoWidth/this.width);
      this.videoRef.nativeElement.width = this.width;
      this.videoRef.nativeElement.height = this.height;
      this.canvasRef.nativeElement.width = this.width;
      this.canvasRef.nativeElement.height = this.height;
      this.streaming = true; 
    }
  }

  clearPhoto() {
    let context = this.canvasRef.nativeElement.getContext('2d');
    context.fillStyle = "#AAA";
    context.fillRect(0,0,this.canvasRef.nativeElement.width, this.canvasRef.nativeElement.height);

    var data = this.canvasRef.nativeElement.toDataURL('image/png');
    this.photoRef.nativeElement.src = data;
  }

  takePicture() {
    let context: CanvasRenderingContext2D  = this.canvasRef.nativeElement.getContext('2d');

    if (this.width && this.height) {
      this.canvasRef.nativeElement.width = this.width;
      this.canvasRef.nativeElement.height = this.height;
      context.drawImage(this.videoRef.nativeElement, 0, 0, this.width, this.height);

      let fd = new FormData();

      this.canvasRef.nativeElement.toBlob((blob) => {
        let url = URL.createObjectURL(blob);
        this.photoRef.nativeElement.onload = function() {
          URL.revokeObjectURL(url);
        };
        this.photoRef.nativeElement.src = url;

        fd.append('image', blob, "myPicture");
        fd.append('timeStamp', Date.now().toString());
        console.log("Uploading: " + JSON.stringify(fd));
        try {
            this.http.post("http://localhost:3000/selection/test-photo",fd)
            .subscribe(
              (res) => {
                console.log("Successful result: " + JSON.stringify(res))},
              (err) => {
                console.log("Subscribe error: " + JSON.stringify(err))} 
          );
        }
        catch(e) {
          console.log("Caught error: " + e);
        }

      }, 'image/png')

    } else {
      this.clearPhoto();
    }
  }

}

后端(Express)

exports.selection_test_photo = [
    (req,res,next) => {
        const photo = new Photo();
        console.log("Entering Post: " + util.inspect(req.file) + "; " + req.body.timeStamp);        
        photo.photo.data = fs.readFileSync(req.file.path);
        photo.photo.contentType = 'image/png';
        photo.timeStamp = {"value": req.body.timeStamp};
        console.log("About to save . . . ");
        photo.save(function(err){
            if (err) {return next(err)};
            res.json({"foo": "bar"});
        });        
    },

];

有没有人遇到过这个问题?有任何想法吗?谢谢!

最佳答案

https://bugzilla.mozilla.org/show_bug.cgi?id=1588939 说:

"Evidently two instances of getUserMedia({video:true}) cannot exist at the same time."

关于angular - 获取 MediaStreamError { 名称 : "AbortError", 消息 : "Starting video failed", 约束 : "", 堆栈 : "" } on one PC, 但不在另一个上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56640137/

相关文章:

mysql - Nodejs/express 从客户端查询 MySQL 数据的最佳方式

javascript - Node.js Express 删除 req.body 不起作用

c++ - 从资源加载图像不起作用(C++ Dll)

angular - 向 Select 上的选项添加操作(Angular 4)

angular - CookieXSRFStrategy 在 AOT 模式下不工作 @angular

angular - 如何根据 Angular 2中的按钮单击移动div滚动位置

node.js - Angular 6 ssr页面负载太大

node.js - 在 SailsJS 中设计更薄的 Controller

opencv - 使用 openCV 2.4.3 从罗技 c920 以 30fps 捕获 1080p

python - 是否可以在 Windows 10 上禁用网络摄像头的自动对焦?