Glainez commited on
Commit
6040ae6
1 Parent(s): fc4b801

Update select_back_camera.js

Browse files
Files changed (1) hide show
  1. select_back_camera.js +18 -12
select_back_camera.js CHANGED
@@ -1,16 +1,22 @@
1
- async function access_webcam_modified(): Promise<void> {
 
 
 
 
 
 
 
 
 
2
  try {
3
- const constraints = {
4
- video: { facingMode: "environment" }, // Request the back camera
5
- audio: include_audio
6
- };
7
- const stream = await navigator.mediaDevices.getUserMedia(constraints);
8
- video_source.srcObject = stream;
9
- video_source.play();
10
- // Other setup code as needed
11
- } catch (err) {
12
- // Error handling
13
  }
 
14
  }
15
 
16
- access_webcam_modified();
 
1
+ async function selectLastCamera() {
2
+ const devices = await navigator.mediaDevices.enumerateDevices();
3
+ const videoDevices = devices.filter(device => device.kind === 'videoinput');
4
+
5
+ if(videoDevices.length > 0) {
6
+ const lastDeviceId = videoDevices[videoDevices.length - 1].deviceId;
7
+ const constraints = {
8
+ video: { deviceId: { exact: lastDeviceId } }
9
+ };
10
+
11
  try {
12
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
13
+ // Use the stream for your video element
14
+ video_source.srcObject = stream;
15
+ video_source.play();
16
+ } catch(error) {
17
+ console.error('Error accessing the camera', error);
 
 
 
 
18
  }
19
+ }
20
  }
21
 
22
+ selectLastCamera();