- I have a web application that opens front camera and displays feed.
- Camera is active and user goes back to iPhone’s home screen (making browser deactivated / visibility changed to not-visible)
- When user resumes application, camera is shown in different aspect ratio – I guess switched between landscape to portrait mode.
I tried following things – none worked
- Open camera with specific aspect ratio
- Opening camera without any constraints.
- Opening camera with fixed constraint.
Here is sample camera application I created for your reference (attaching code as jsfiddle did not allowed me to open camera).
Open this page locally / or secured link on iPhone. Start camera and check aspect ratio. Go to iPhone’s home. Switch back to application. Notice camera and its aspect ratio logged.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Camera Feed with Settings</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 20px;
}
video {
border: 2px solid #000;
width: 500px;
height: 375px;
}
div {
margin-top: 10px;
padding: 10px;
border: 1px solid #000;
width: 500px;
background-color: #f0f0f0;
}
button {
padding: 10px 20px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<button id="startCameraBtn">Start Camera</button>
<video id="videoFeed" autoplay playsinline></video>
<div id="trackSettings">Camera settings will appear here.</div>
<script>
const startCameraBtn = document.getElementById('startCameraBtn');
const videoFeed = document.getElementById('videoFeed');
const trackSettingsDiv = document.getElementById('trackSettings');
let mediaStream = null;
// Function to display video track settings
function displayTrackSettings() {
if (mediaStream) {
const videoTrack = mediaStream.getVideoTracks()[0];
const settings = videoTrack.getSettings();
trackSettingsDiv.textContent = JSON.stringify(settings, null, 2);
}
}
// Start the front camera
startCameraBtn.addEventListener('click', async () => {
try {
mediaStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'user' } });
videoFeed.srcObject = mediaStream;
displayTrackSettings();
} catch (error) {
console.error('Error accessing camera: ', error);
trackSettingsDiv.textContent = 'Error accessing camera.';
}
});
// Update settings when page visibility changes
document.addEventListener('visibilitychange', () => {
displayTrackSettings();
});
</script>
</body>
</html>
2