1
votes

I created a jsx variable to embeds a video into my html. Every other answer says to include muted, defaultMuted, and playsinline (which I already have). The videos autoplay in safari, chrome and firefox on my computer, but not on mobile. The start screen of the video loads, but it is paused. Do I need to do it slightly differently because I'm using React maybe?

I'm using an iPhone on iOS 13.3, the autoplay isn't working on safari, chrome and firefox, but only on mobile. The videos are all .mp4(.mov files also don't work).

var EmbedVideo = function(props) {
    return (
        <video webkit-playsinline playsinline autoplay="autoplay" className={props.className} muted defaultMuted loop>
            <source src={props.src} type="video/mp4" />
            Your browser does not support the video tag.
        </video>
    )
}

Update

So apparently 'muted' doesn't show up when I inspect the html of my website. The node looks like this. There's a few attributes that are missing actually.

<video autoplay="" class="video" loop="">
<source src="/videos/my_video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

I'm reading something about the muted attributed not working with React? Someone made a component that looks like it's the video tag, but functioning how it's supposed to(at least in my case with videos playing like gifs). I can't get it working though, it's not even autoplaying on Desktop. I'm trying just <VideoTag src={props.src} /> because I don't know what their poster variable is supposed to be.

3
I think you should use autoPlay not autoplay - bkm412
@bkm412 Didn't work. I tried autoplay="autoPlay", autoPlay="autoplay", and autoPlay="autoPlay". Which one did you mean? - Sam
@bkm412 which one do you mean, it's possible that I messed something else up when attempting, or there was a cache issue or something, I don't want to try all three repeatedly when I'm testing these separate issues - Sam
If I recall correctly the default behavior on mobile is to ignore autoplay in order to conserve bandwidth. - j08691
@j08691 Could you explain this more? I just got it working by using dangerouslySetInnerHTML. I was using gifs before, but some of the gifs were about 20 MB, and lower quality. The corresponding mp4's are about 2 MB maximum and most were about half a MB, and better quality so it just seems like a much better option. Plus isn't the mp4 already downloaded when it's displayed with the html?(it's not an embedded youtube link) - Sam

3 Answers

3
votes

It looks like muted does not work properly when using React. I had to use something called dangerouslySetInnerHTML in order for muted to show up in the component.

var EmbedVideo = function(props) {
   return (
       <div dangerouslySetInnerHTML={{ __html: `
        <video
          loop
          muted
          autoplay
          playsinline
          src="${props.src}"
          class="${props.className}"
        />,
      ` }}></div>
   )
}
0
votes

I just had the same issue and thought I'd share my solution, hoping that it helps others and saves you time and many headaches.

So, I created a component for the video and added in autoPlay and playsInline, paying special attention to capitalization.

Here's the JSX code that I used:

<VideoBG 
    autoPlay={true} 
    loop={true}
    controls={false} 
    playsInline
    muted 
    src="../../videos/video4.mp4" 
    type="video/mp4" 
 />

Here's the styling (styled components, but basically css) that I used:

export const VideoBG = styled.video`
    width: 100%;
    height: 100%;
    -o-object-fit: cover;
    object-fit: cover;
`;

The ={true} might not be necessary, but just in case, this worked for me.

-4
votes

I think when you embed a video make sure to use the right path