Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML5 video tag support muted parameter #6544

Closed
cometta opened this issue Apr 19, 2016 · 51 comments
Closed

HTML5 video tag support muted parameter #6544

cometta opened this issue Apr 19, 2016 · 51 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@cometta
Copy link

cometta commented Apr 19, 2016

I am unable to use muted parameter for <video autoplay muted loop>.. . Other parameters for video seems fine.

@jimfb
Copy link
Contributor

jimfb commented Apr 19, 2016

Appears to work fine for me: http://jsfiddle.net/314xxopf/

@jimfb jimfb added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 19, 2016
@GTVanitha
Copy link

@cometta, which browser you are using?

@cometta
Copy link
Author

cometta commented Apr 19, 2016

you are right. i retested it. sorry. I proceed to close it.

@cometta cometta closed this as completed Apr 19, 2016
@moret
Copy link

moret commented May 9, 2016

Strangely enough, I ran into the same issue today, on different Chrome versions. I tested with the fiddle above and the muted attribute is unavailable:

screen shot 2016-05-09 at 14 09 08
screen shot 2016-05-09 at 14 10 41

@moret
Copy link

moret commented May 9, 2016

I expanded a bit @jimfb's fiddle at http://jsfiddle.net/wmzjzjmm/ to check server rendering and tested in Safari as well. renderToString rendered both muted and controls attributes, despite only the controls attribute showing up when using render:
screen shot 2016-05-09 at 14 42 41
screen shot 2016-05-09 at 14 43 43

The video is muted despite the attribute not showing up, but I can only access the attribute when I'm resuming a server render.

@jimfb
Copy link
Contributor

jimfb commented May 9, 2016

@moret When I run the code in your fiddle, the video appears to be muted (no sound). That's the only thing that React guarantees; React is not required to set the attribute if it is not required.

@zpao
Copy link
Member

zpao commented May 9, 2016

When rendering on the client we'll use the property since it was determined that modifying the attribute didn't actually affect the video. We must set it as node.muted = true|false, so you don't see that reflected in the attribute.

@moret
Copy link

moret commented May 9, 2016

@zpao makes sense, and that explains well the difference between the seemingly similar attributes. Thanks for the explanation!

@aza
Copy link

aza commented Nov 7, 2016

@zpao ohai, and also it seems that Mobile Safari in iOS10 uses the video's muted attribute to determine if the video can autoplay: https://webkit.org/blog/6784/new-video-policies-for-ios; having video.muted == true isn't enough.

@lawlmart
Copy link

I'd argue this needs to be reopened. This breaks iOS10 autoplay support.

@zpao
Copy link
Member

zpao commented Nov 18, 2016

<video muted> elements will also be allowed to autoplay without a user gesture.

and

<video> elements will be allowed to play() without a user gesture if their source media contains no audio tracks, or if their muted property is set to true.

These conflicting statements lead me to believe we can (and probably should) continue using the property. We might just need to ensure that muted is set before autoplay.


Can somebody having an issue share the code they're having an issue with (hi @aza!) - I can't repro the problem but I could be looking at the wrong thing. Here's what I have:

ReactDOM.render(
  <video width="100%" controls autoPlay muted playsInline>
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4" />
  </video>,
  document.getElementById('container')
);

This is rendering the video and autoplaying inline without any issue (iOS 10.1 in the simulator with React 15.4). If I leave off muted then it will not autoplay.

@mschipperheyn
Copy link

mschipperheyn commented Mar 14, 2017

I have the same issue: react 15.4.2 + SSR

@kohlmannj
Copy link

kohlmannj commented Apr 12, 2017

I'm (begrudgingly) working around this issue by using dangerouslySetInnerHTML:

const VideoWorkaround = ({ src }) => (
  <div dangerouslySetInnerHTML={{ __html: `
    <video
      muted
      autoplay
      playsinline
      src="${src}"
    />
  ` }}
  />
);

This issue should absolutely be reopened.

@tconroy
Copy link

tconroy commented Apr 26, 2017

this is quite odd.

With both the regular JSX syntax and the template-string version posted by @kohlmannj, I'm seeing the muted attribute stripped off of the node. All other attributes are kept in tact.

      <video
        muted
        playsInline
        preload
        loop
        autoPlay
      >
        <source src={this.state.src} type="video/mp4" />
      </video>

Any ideas what is going on here? This seems to work ok in iOS10, however Android seems to drop the muted attribute and just not play.

if I try to invoke play ie this.elementRef.play(), I get the must be initiated as a result of user interaction error.

Emulated Pixel on Android 7, mobile Chrome v.51.0.02704.90

@tconroy
Copy link

tconroy commented May 23, 2017

Has anyone gotten autoplay video to work on Android with React?

Seems to work on iOS 10 with playsInline + autoPlay but cannot get it to work consistently on Android. Is this limited by Android version?

@Swizec
Copy link

Swizec commented Oct 2, 2017

This is still an issue. Here was my original code:

const Video = ({ src, onEnded }) => (
  <video src={src} autoPlay playsInLine muted onEnded={onEnded} ref="video"/>
)

It would load videos but wouldn't play them on iOS.

I fixed it by doing this, but it produces a laggy experience because the video doesn't play immediately.

class Video extends Component {
    componentDidMount() { this.update(); }

    componentDidUpdate() { this.update(); }

    update() {
        this.refs.video.setAttribute('muted', '1');
        this.refs.video.setAttribute('playsinline', '1');
        this.refs.video.setAttribute('autoplay', '1');
    }

    render() {
        const { src, onEnded } = this.props;

        return (
            <video src={src} autoPlay playsInLine muted onEnded={onEnded} ref="video"/>
        )
    }
};

You can see the difference by going here on both iOS and Desktop. I haven't tried on Android.

@DylanVann
Copy link

I made a package to render a video tag using dangerouslySetInnerHTML.

It's pretty specific to my use case of animated gif like videos but feel free to send a PR or open an issue if you want something else.

https://github.com/DylanVann/react-video-tag

@diuis
Copy link

diuis commented Oct 5, 2018

I enabled autoplay on iOS and React 16 with the videoelement defaultMuted property: https://gist.github.com/diuis/fc1c2f0e255538cda397c5a6b669c9bf

@jpdevries
Copy link

Um. We need the muted attribute in the HTML.

No limitation on autoplay muted video
https://medium.com/@sundaykuloksun/video-play-not-working-html5-autoplay-policy-beed81d64ca5

@lewisdonovan
Copy link

Thanks @kohlmannj that worked for me. This issue needs to be fixed though, it's well documented that muted is required for auto playback on iOS.

@zoubingwu
Copy link

zoubingwu commented Oct 19, 2018

ran into the same issue today.

<div>
  <video autoPlay loop muted playsInline>
    <source src={xxx} type="video/mp4" />
  </video>
</div>

only muted attribute was missing in real DOM.

create-react-app with react 15.5.4

@gen1321
Copy link

gen1321 commented Nov 8, 2018

The issue should be reopened. I have the same problem

@vedadeepta
Copy link

Any update on this , facing this issue on react: 16.4.1.

@uguryilmaz-noxarc
Copy link

this is definitely happening. On chrome its OK. but on safari muted attribute is gone missing somehow. that causes unable to autoplay video

@davidhariri
Copy link

I'm having this issue as well.

@tomasdev
Copy link

REOPEN.

@cchawn
Copy link

cchawn commented Jan 21, 2019

Plus one – running into this issue as well
react 16.6.3

@MikuZZZ
Copy link

MikuZZZ commented Jan 21, 2019

Same problem here.
React 16.4.1

@claygiffin
Copy link

claygiffin commented Jan 21, 2019

Also running into this same issue.
React 16.7.0

@spoontstr
Copy link

same

@giles-v
Copy link

giles-v commented Jan 24, 2019

For others looking for this issue to be reopened - #10389 is probably the best issue to watch.

@tomasdev
Copy link

tomasdev commented Apr 2, 2019

Proper solution should be to remove the audio track from the video.

If you can't, then create a separate component, so it doesn't update every time state or props change.

class Video extends React.Component {
  shouldComponentUpdate(nextProps) {
    return nextProps.src !== this.props.src;
  }

  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: `
        <video
          loop
          muted
          autoplay
          playsinline
          src="${this.props.src}"
        />,
      ` }}></div>
    );
  }
}

Saqoosha added a commit to Whatever-Co/whatever.co-2019 that referenced this issue Apr 17, 2019
@amishPro
Copy link

I have the same problem.

@keithpickering
Copy link

Seriously guys? There's no way autoplaying muted videos on mobile is an edge case. I don't want to use animated GIFs because it's not 1999.

@jonlow
Copy link

jonlow commented Aug 21, 2019

Still an issue. Like others, I had to use the dangerouslySetInnerHTML option to get it working

@coryetzkorn
Copy link

I'm still experiencing this issue. muted attribute is being removed from video elements.

@scchess
Copy link

scchess commented Nov 13, 2019

Yes, this is still an issue. Please fix it.

@joe-lloyd
Copy link

Can't use the dangerouslySetInnerHTML option because I need a ref to control it.

@duchu-net
Copy link

Issue still exist.

@joeLloyd - You can ref from parent ;)

dangerouslySetInnerHTML={{ __html: `<video>...</video>` }}
ref={ref => ref.children[0]} // <- it's video ref

@kasiaosinska
Copy link

Same problem here.
React 16.8.6.

@chapl
Copy link

chapl commented Feb 5, 2020

I'm also having this problem although only on safari on iphone, the problem is reproducible. It's working on windows, android and mac in all the browsers I've tried. Stripping the audio track didn't work either.
If there's a reasonable work around or a github with a working feature please let me know.
Cheers

@cibulka
Copy link

cibulka commented Feb 27, 2020

Issue still exists on Safari, OSX 10.15.3, React 16.12.0.

@FerusAndBeyond
Copy link

Shouldnt this be reopened, such a critical issue isn't fixed for 4 years?

@alevosia
Copy link

A few eons later and React developers are still unable to autoplay background videos natively..

@loopmode
Copy link

Well.. I don't see the muted attribute in the DOM using devtools (Chrome, Firefox), but the video is definitely muted and we do have a working autoplay in our app.
Selecting the DOM node of the video and going $0.muted in the console does give true as well.
For us, it's just that the "muted" attribute is not shown in the DOM tree view.

There might be other aspects that prevent your autoplay functionality.

@boltcoder
Copy link

dangerouslySetHtml + playsInline+muted+autoplay+loop + removing audio from mp4 audio everything fails for ios 13.5.1. I've literally wasted 6 hours till now.

@boltcoder
Copy link

dangerouslySetHtml + playsInline+muted+autoplay+loop + removing audio from mp4 audio everything fails for ios 13.5.1. I've literally wasted 6 hours till now.

People who are just trying to autoplay muted (usually in banner background ). I wrote a medium article which can save you hours of googling.
https://medium.com/@BoltAssaults/autoplay-muted-html5-video-safari-ios-10-in-react-673ae50ba1f5

Hope this helps!

@lucashfreitas
Copy link

lucashfreitas commented Aug 17, 2020

4 years later and facing the same issue here.

<video muted is stripped out from the HTML.

Quite surprised as this feature was not considered in 4 years given that it is really necessary for who is developing a website with a lot of background/autoplay videos.

See: https://webkit.org/blog/6784/new-video-policies-for-ios/

There is any specific reason to not implement this? Would a PR be welcomed?

@ghost
Copy link

ghost commented Oct 8, 2020

@diuis

Converted your solution into a more concise one with hooks. Found yours to be the best one as it does not use dangerouslySetInnerHtml

props is expecting a 'video' prop containing the path to an mp4 file in the public directory, and 'className' containing any extra styling wanting to be done to the video (width, height, max-height, etc)...

export default function AutoPlaySilentVideo(props) {
    const videoRef = useRef(undefined);
    useEffect(() => {
        videoRef.current.defaultMuted = true;
    })
    return (
    <video className={props.className} ref={videoRef} loop autoPlay muted playsInline>
    <source
      src={props.video}
      type="video/mp4"
    />
  </video>);
}

@gaearon
Copy link
Collaborator

gaearon commented Oct 8, 2020

The problem here is that instead of filing a new issue with a reproducing case, the discussion is happening in a closed issue that no one is reading (because it is a closed issue). If this is still a problem please file a new issue with a repro case and we'll take a look.

@facebook facebook locked and limited conversation to collaborators Oct 8, 2020
@gaearon
Copy link
Collaborator

gaearon commented Oct 8, 2020

I'm locking so that if the issue is relevant, someone creates a new report.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests