I made a react web which has a video as background image. It works on PC but not on mobile, at least not on my iphone. After searching for hours, edited autoplay, playinline etc. in several different ways, nothing changes. On mobile the web only shows an empty background.
I use absolute position and zIndex to make it run as a background image.
My code is as below.
<video
id="video"
width="100%"
height="100%"
autoPlay="autoplay"
playsInLine="playsinline"
loop="true"
muted="true"
style={{
position: "absolute",
zIndex: -1,
opacity: 0.5,
objectFit: "cover",
}}
src={bgv}
type="video/mp4"
></video>
2
Answers
The issue you’re experiencing with the video not playing on mobile devices, especially iPhones, is quite common due to several restrictions and requirements imposed by mobile browsers. Specifically, mobile browsers often require user interaction (like a tap) to start playing media files due to data and battery usage concerns.
Your code appears correct in terms of attributes, but there are a few adjustments and best practices you should consider:
Boolean Attributes: For
autoPlay
,playsInLine
,loop
, andmuted
attributes, you should use them without values as they are boolean attributes in HTML.Video Controls: Add a fallback message or consider showing a play button that users can interact with to start the video.
Your updated code might look something like this:
Additionally, you can ensure that the video starts playing when the page loads by adding an event listener in React.