长话短说:如果您想在旧设备上选择不支持 faceMode 约束的后置摄像头 - 您需要sourceId: { exact: device.id }
在视频中使用约束:{} config.
长:
export interface SourceInfo {
facing: string; // "environment"
id: string; // "bcb8d32aebb99fdf1d5f2fdb4ec4ec28a381b83f5e3c96cbcb30c4ab757380da"
kind: string; // "video"
label: string; // ""
}
代码(打字稿):
(navigator as any).getUserMedia =
(navigator as any).getUserMedia ||
(navigator as any).webkitGetUserMedia ||
(navigator as any).mozGetUserMedia ||
(navigator as any).msGetUserMedia;
if (navigator.getUserMedia && (window as any).MediaStreamTrack) {
(MediaStreamTrack as any).getSources((sources: SourceInfo[]) => {
this.videoSources = sources.filter((source: SourceInfo) => {
return source.kind === 'video';
// or source.facing === 'environment'
});
// console.log('got video sources', this.videoSources);
try {
const rearCameraDevice = this.videoSources.find((device: SourceInfo) => device.facing === 'environment');
const anyCameraDevice = this.videoSources[0];
const constraints = {
video: {
mandatory: {
sourceId: rearCameraDevice.id || anyCameraDevice.id
}
// these both not work with old constraints...it's new syntax
// deviceId: this.videoSources[0].id
// deviceId: { exact: this.videoSources[0].id }
}
};
navigator.getUserMedia(
<any>constraints,
this.successOldStream.bind(this),
this.errorOldStream.bind(this)
);
} catch (error) {
console.error(error);
}
});
} else {
console.error('your browser not supported camera/media capturing')
}