在Android设备上翻转相机时WebRTC黑屏

时间:2017-07-18 04:41:51

标签: android camera ionic2 webrtc

我正在使用离子2和webrtc从前后摄像头获取视频流。

请参阅下面的打字稿代码:

if (this.isFrontCam) {
  constraints = {
    mandatory: {},
    optional: [{sourceId: this.cameras[0]}]
  };
} else {
  constraints = {
    mandatory: {},
    optional: [{sourceId: this.cameras[1]}]
  };
}


if (this.currentVideoStream && this.currentVideoStream !=null) {
      this.currentVideoStream.getTracks().forEach(function (track) {
        track.stop();
      });
      this.currentVideoStream.release();
      this.currentVideoStream = null;
    }



 var n = <any>navigator;
    n.getUserMedia = n.getUserMedia || n.webkitGetUserMedia || n.mozGetUserMedia || n.msGetUserMedia;

    //getting local video stream
    n.getUserMedia({
      audio: true,
      video: constraints
    }, function (myStream) {
      alert("Current Video stream " + self.currentVideoStream);
      self.currentVideoStream = myStream;
      alert("New Stream"+ myStream);
      //displaying local video stream on the page
      (<HTMLVideoElement>document.getElementById('localVideo')).src = window.URL.createObjectURL(myStream);

我正在获得2个不同的(前置和后置)摄像头设备ID(this.cameras),如果我单独使用它们,两个摄像头都按预期工作,但是当我使用上面的代码翻转它们时(从UI调用点击按钮)这个功能)就像上面他们不工作。它只是显示黑屏。

1 个答案:

答案 0 :(得分:0)

我能够通过删除以下2行来解决这个问题:

this.currentVideoStream.release();
this.currentVideoStream = null;

并添加(<HTMLVideoElement>document.getElementById('localVideo')).play();