我正在使用带有webrtc的ionic 2从前后摄像头获取视频流。
请在下面查看我的打字稿代码:
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 中的按钮单击调用此功能(如上所示,它们不起作用。它只是显示黑屏。
我能够通过删除以下 2 行来解决此问题:
this.currentVideoStream.release();
this.currentVideoStream = null;
并添加(<HTMLVideoElement>document.getElementById('localVideo')).play();